×

Search anything:

Viewing the history of git commits (git log)

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Reading time: 30 minutes | Coding time: 10 minutes

Follow a few steps to get along with this tutorial:

  1. Create a directory named git_view_history. Create a python file named hello.py.
  2. Initialize git in this repository. Using command git init.
  3. I have created 3 commits in the repository and it looks as follows:

commit 1:

git commit -m "first commit"
[master (root-commit) 981d5a8] first commit
1 file changed, 1 insertion(+)
create mode 100644 hello.py

in this commit, the code simply prints hello world.

print("hello world")

commit 2:

git commit -m "changed greetings"
[master 8d833eb] changed greetings
1 file changed, 2 insertions (+) , 1 deletion (-)

in this commit, the code prints hello readers with readers stored in a variable.

greetto = "readers"
print("hello "+greetto)

commit 3:

git commit -m "add print statement for history"
[master 642bf08] add print statement for history
1 file changed, 3 insertions (+) , 1 deletion (-)

in this commit, the code prints another statement.

greetto = "readers"
print("hello "+greetto)
print("let's see how to view history of commits")

Now, let's see how to view the history of commits:
git log command shows commit hash, author, date, and the commit message.

git log
commit {hash of the commit}
Author: NikitaMasand {email of author}
Date: Sun May 31 13:38:47 2020 +0530
add print statement for history

commit {hash of the commit}
Author: NikitaMasand {email of author}
Date: Sun May 31 13:38:47 2020 +0530
changed greetings

commit {hash of the commit}
Author: NikitaMasand {email of author}
Date: Sun May 31 13:38:47 2020 +0530
first commit

There are many options we can use with log, we will focus on option pretty.
For one line format of commits consisting of hash and commit message, we can use oneline option with pretty as follows:
command:

git log --pretty=oneline

git log --pretty=oneline
{hash of the commit} add print statement for history
{hash of the commit} changed greetings
{hash of the commit} first commit

We can get commits by limiting the number of commits shown in the output:
the following snip gets maximum of 2 recent commits, thus the very first commit is not shown.
command:

git log --pretty=oneline --max-count=2

git log --pretty=oneline --max-count=2
{hash of the commit} add print statement for history
{hash of the commit} changed greetings

We can also get commits since some time ago. For example:
command:

git log --pretty=oneline --since='23 minutes ago'

git log --pretty=oneline --since='23 minutes ago'
{hash of the commit} add print statement for history

command:

git log --pretty=oneline --since='25 minutes ago'

git log --pretty=oneline --since='25 minutes ago'
{hash of the commit} add print statement for history
{hash of the commit} changed greetings

Working with many people, you could also list down the commit history of a particular author as:
command:

git log --pretty=oneline --author= [Author Name]

git log --pretty=oneline --author=NikitaMasand
{hash of the commit} add print statement for history
{hash of the commit} changed greetings
{hash of the commit} first commit

In our case, there was only one author. This is mostly useful in case we want to list down the commits when there are a lot of people in the contributors list.Why don't you try viewing your commits in a group project ? If not, you can randomly take one open source project (openGenus/cosmos is a good idea), take any one author and view all their commits!

git log --pretty=oneline --all  

would print all the things.

You can read about more such options in the official documentation here: git-scm

The most interesting option is format, which allows you to specify your own log output format.
command:

git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
*{642bf08} 2020-05-31 | add print statement for history (HEAD -> master) [NikitaMasand]
*8d833eb 2020-05-31 | changed greetings [NikitaMasand]
*981d5a8 2020-05-31 | first commit [NikitaMasand]

Nice! The output shows the commit hash, date, commit message, author name.
But what does everthing in the command mean ? Let's see.

  • %h: shows the shortened hash of the commit, eg; 642bf08. The commit hash is the SHA-1 hash of the commit object

  • %ad: is the author date, eg: 2020-05-31 in our case

  • | is just used as a separator. As you can see with the output

  • %s: commit message that was added when committing the code, eg: add print statement for history.

  • %d: decorator, shows branch heads, tags if any. eg: (HEAD->master)

  • [ ]: we just want the other parameter inside these brackets like | was used as a separtor, [] are also printed

  • %an : author name, in our case NikitaMasand

  • --graph : asks git to display the commit tree, eg: when you merge two branches in a commit, it could be shown as well. It isn't an actual graph with display, it uses ASCII characters that give a good visual display.

  • --date=short : a date format. if you won't use this, you might get something like this:
    Sun May 31 13:38:47 2020 +0530
    using the short format gives the date in a more readable manner:
    2020 - 05 - 31
    In our case, graph was not useful as we did not form any branch, it can be in other cases. This is the example taken from git-scm link:
    graph

The problem is that sometimes we want to use this command as we want our log to be displayed, do we need to type this long command all the time ?
Thank God, alias command comes to the rescue. We will see more about it in the next article.

In this article at OpenGenus, we mostly saw different ways in which we can view the commit history. If there are any doubts, please post them in the comments section.

Viewing the history of git commits (git log)
Share this