Enroll in Selenium Training

Since in the last few chapters we have gone through learning about how to add and track changes to staging, commit changes in git and seeing the git logs. After doing all of the above, we have got a very familiar idea about working in Git. The above said commands are the most important things in Git.

In this tutorial, we will enhance the git log command to view Git History and present to you some very useful and most used Git Log Commands. Please note that these commands are specific to Git Log only. We will provide you with a Git Cheatsheet once we are done with the important concepts in Git. In this tutorial we will be covering the following:

  • Git Show Command
  • View Commit Log Size
  • View Commit History for a Date range
  • Skipping top Commits
  • View Author Commit History
  • View Commit History in Reverse order
  • View Commit Stats

What is Git Show command?

Git Show command is similar to git log in terms of output. Git show also presents you the output in the same format as we studied in the git log tutorial. A slight difference is that the git show command shows you two things:

  • The commit to which HEAD is pointing
  • Difference between the versions of the file to which HEAD is pointing

The commit to which HEAD is pointing is the last commit of the last branch that you were working on. In this case, I created a new file called "lakshay.txt" (touch command) and wrote a simple line in it and committed the changes. After that I tried to type the command:

git show

Git Show Command

The first part of the git show command that has been marked in the image is similar to the git log command but points towards the HEAD. There is also a second part to the output that the command gave us:

Git Show Command Diff

This part is called the diff. Although it would not be correct to define this here as it will be discussed in detail in Git Diff tutorial. But since we have encountered it here, let me briefly tell you about it. Diff in Git to tell you about the difference between the file that the HEAD is pointing to (By Default). As you can see Git has marked a/lakshay.txt and b/lakshay.txt  in the first line. It is used to denote two specific states of the file lakshay.txt. It will be clear when we learn about it in detail in the Diff tutorial.

If you want to use git show for a particular commit and not as a general command, use the hash value of that commit as shown below.

Git Show Command With Hash

Note: I have used the compressed hash value that comes when we use --oneline option. Refer to the Git Log tutorial to know more.

How to view Git Commit History of specific intervals?

It is a self-explanatory option in git log. When we type the command:

git log --since=<date>

Git Log Since

All the commits happened since that date comes as the output. It will exclude the commit that happened on that date. There is no need to explain it further. It works as a filter in git log.

How to Skip a number of commits from the git log history?

Skip option is used in Git Log to skip a number of commits in your Git Log. Let's first see how git log is shown so that the difference is clear to you. Type git log --oneline to see the list of commits.

Git Log Oneline

Now let us try to skip 4 commits by typing the following command:

git log --skip 4 --oneline

Git Log Skip

Look at the above two images. The skip command has eliminated the four top commits. There is no written purpose as to why skip command was first brought into the picture but its use has been more than average making it one of the important commands.

How to View Commits of a specific Author?

Author option in Git Log is used to filter out all the commits which were done by a particular author. Needless to say, it is a very important command so as to see the commits belonging to one person in your team or maybe all your commits.

To execute the command, type the following:

git log --author=<name of author>

Git Log Author Harish

These are all the commits done by Harish. If I check how many commits are done on this repository by Lakshay then I will just change the author name in the command.

Git Log Author Lakshay

As expected, there have not been any commits by the author name Lakshay.

How to view Git Commit History in chronological order (reverse)?

In the tutorial about Git Log, I discussed the order in which commits are shown when we execute the git log command. They are shown in reverse chronological order i.e. the latest commit will be shown first. It makes sense because as we proceed further into the project in our repository, we are more concentrated on the latest changes. It is obvious that again and again, we will not be looking at the older commits. But if you are wondering how to get the commits in chronological order, then this option in Git saves you. To see the commits in chronological order, type the command:

git log --reverse

Git Log Reverse

Note: See the Head pointer is the last to show now. Also, note that I have been using --oneline option to provide everything in a short and clear way. Please refer to the Git Log tutorial for more information.

How to view Stats of Commit?

Stat command in git log is used for viewing the summary of the files. We will see what the command is after seeing the output. For this, type:

git log --stat <commit id>

Git Log Stat

As a practice, just think why I used an alphanumeric word after stat.

As you see, after the commit message, it shows the file name Harish.txt along with the symbol 1+. In this symbol, the digit represents the total number of operations and the symbol (+ in this case) denotes the operation performed. So here 1 operation has been performed which is insertion as also mentioned below the line.

How to view log size in git history?

Log Size in Git is an option in Git Log to tell you about the log-size in a digit. It outputs an additional line with log-size <number> as shown in the below image after typing the command:

git log --log-size

Git Log LogSize Command

Note: I have shown you the last commit only. You will get a sequence of commits in reverse chronological order.

As you can see, there is an additional line printed on the console called log size and a number. So, this number is the commit message length in bytes. It is required by various tools that read commit messages through git log command. By reading this number they can allocate exact space in advance for saving the commit message. Remember we discussed in writing good commit messages to keep your message short? Well, it saves memory here.

So, these were some of the most useful commands in Git Log. Remember git log is as useful as git commit because you will regularly be seeing the history of your commits and that is the main focus of Git, that you can see the history of commits. I would like you to practice these commands in different ways and as much as you can. We will move on to our next tutorial.

GitHub Introduction
GitHub Introduction
Next Article
Harish Rajora
I am a computer science engineer. I love to keep growing as the technological world grows. I feel there is no powerful tool than a computer to change the world in any way. Apart from my field of study, I like reading books a lot and developing new stuff.
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles