Enroll in Selenium Training

Git Log In the dot git folder tutorial, we learned that Git records everything that happens in its repositories. And why it shouldn't? After all, it is a Version Control System and it has to be efficient in it. The term which captures all the changes happening inside the repository Git Log is the same as it is used in Database and in normal life. A logger in terms of computers means a device or computer program that records the events or observations or measurements. In Git also, this process is called logging and as we will study in the next section, we use git log command for accessing the logs. This tutorial will constitute the following:

  • What is Git Log?
  • Git command to view commit history
  • Git command to view commit history for a file
  • Git command to view commit history of a range
  • Git command to view a limited number of commit history

What is Git Log Command?

Git log is a command used in Git to access the history of commits that the repository has gone through. A simple log command is executed by typing the following command inside git:

git log

Git Log

Note: I have used this image as a reference in the complete tutorial below. Please keep this in mind.

As you can see not only the commit history with the commit identifier is shown. These commits are shown in reverse chronological order (the last commit will be shown on the top). It also includes various other information which is extremely useful since the Git repository is not used by a single person. A Git repository is also accessed and used by many remotely. This running log of commits contains the following four pieces of information:

  1. Commit Hash: The first part of the commit log is "commit hash" which is the hash value by which Git saves or refers everything internally. Refer to the Dot Git folder to know more.
  2. Commit Author: This part tells you about who committed the changes in the repository i.e. the author name.
  3. Commit Date: This part shows you the date and time of the commit with the day and time zone. It is important to have a day and time zone because many people work on a single project from different geographical locations.
  4. Commit Message: This part shows the commit message that was written while committing the changes. Refer to this tutorial to learn how to write good commit messages.

Obviously, not everyone would like to see a long list of commits. Everyone has their own preference and Git takes care of this. Git log has many options that help to filter out the commit history according to you and giving some power to the Git Log. Let's see those options.

How to view Squashed Commit History with Git Log?

Line option in git log is used for viewing the condensed view of the commit history that we just saw above. To see how it looks, type the following command and press enter:

git log --oneline

Git Log OneLine Command

Note: It is a double hyphen before oneline, which you can see in the screenshot.

As you can see some of the details that were previously visible are now hidden and the view is of only one-liner. This is much understandable since every commit is now shown in a single line, starts with Commit Id followed by Commit Message.

  • Commit Id: Every commit has a commit id which is actually a compressed version of commit hash. Since a commit hash is not a sequential number, compressing it still shows unique commits.

  • Commit Message: It is clear with a message (commit message to understand what the commit was about) and short.

How to view Commit History by Commit ID with Git Log?

Now, you must be wondering as a developer that you executed oneline command so that you can locate your required commit through the message. Now, you want to see the details of it. It is not showing in oneline and you don't want to go through the headache of git log command and locate again. To solve this, type the following command:

git log <commit hash>

Git Log Hash

Note: The commit hash you use will be used as a start hash. All the commits did before that will be shown as usual.

How to view Commit History of a File by Git Log?

Log command produces the commit history in reverse chronological order. These commit histories are of the repository and a single repository may contain many files. Log command provides an option to view the commit history of a particular file. This option is <filename>. A specifier maybe. To execute, type the following command:

git log <filename>

Git Log Filename

Note: These are the commits that have been done on the file named harish.txt.

How to view Commits History for a range?

Specific commit history refers to the history that is from a defined commit to another defined commit. As we saw in the above section, commit history contains an id for every commit shown. This is called a commit identifier. A unique id for the commit. To execute the same, type the following command:

git log <since commitId>..<until commitId>

Log Since Until Command

Since and until will have the identifier of the git commit (id). As you can see in the results, it does not include the "since" part of the id but includes the "until" part. You can also see that even though since comes first does not mean you have to enter the latest commit.

  • Since means, the last commit up to which you want to see.
  • Until means, the latest commit that you would like to see.

Remember that we see reverse chronological order in Git and not normal chronological order. As a practice, I would like you to interchange since and until ids above and see how Git Bash responds to it.

Combine oneline option with since..until to have a compressed view of the same.

Log Since Until with Oneline

How to view a limited number of Git Commits History?

Commits can also be viewed by assigning a specific number. For example, we can view the last 4 commits that happened in our repository. It is a very simple git command which needs no explanation.

git log -n 3 -oneline (issue last 3 git commits)

Log Specific Commits

As you can see, you have got the last three commits with one-line descriptions.

Git log is a very important command in this course and your Git learning journey. Git log helps you see the past commits which helps to see who did what in Git and the repository. It helps you track the changes that happened in your repository. Mix the options with the commands to explore more in the Git log. We will head on to our next tutorial.

Git Status Command in Git
Git Status Command in Git
Previous 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.
Reviewers
Lakshay Sharma's Photo
Lakshay Sharma

Similar Articles

Feedback