Signup/Sign In

Git Log

Git is used to save different versions of our project in the form of snapshots stored in commits. The Git log command is used to view the commit history of our repository.

There are a lot of different flags and options that can be used with the Git Log command to view the history in different formats.

In its original form, the Git log command will simply output the commit hash, the author details, and the commit message. Let's learn more about the Git Log command.

Git Log Command

When used without any additional flags or options, the Git Log command will output the commit hash, the author name and email, the date and time when the commit is made, and the message associated with the commit.

There may be some additional bits of information like the branches present in the commit history, or tags that were created on the commits.

$ git log

Consider the following image that shows the output for the Git Log command.

Viewing the output of Git Log command

The Git log command can be used to format the commit history so that it can be easily understood. We can also filter out the history to view only a certain part of it based on some conditions. Let's learn about different flags and options that be used to format and filter the history.

Formatting the Commit History

We can format the commit history using the Git Log command and view the output in different ways. Let's look at the different ways in which we can format the output of the Git Log command.

View One Commit per Line

The output of the Git Log command gives a lot of information and it may be difficult and overwhelming to understand the entire history. We can use the --oneline option with the Git Log command to see just a single line per commit. In this format, only the first 7 letters of commit hash are displayed along with the commit message.

$ git log --oneline

The output of the above command will look like the following.

Output of the Git Log command with the --oneline option

View Git Diffs for the Altered Files

A commit will include many files and we can view the changes that were made to each file in a diff format by using the -p or the --patch option.

$ git log -p

Consider the following case where the most recent commit of our repository had two files in it. The Git Log -p command will output the changes made to each of these files.

Viewing the output of Git Log command with the -p flag

View the Summary of Changes Made

The Git Log command with the --patch option is helpful if we want to view the changes made to the files. But sometimes we may just want a brief summary of what was done to the files included in the commit. In such a case, we will use the --stat option. It will tell us how many lines were added and deleted to the files.

$ git log --stat

The output of the command is shown in the image below.

Output of Git Log command with the --stat option

View the Commit History in the form of Graph

We can use the --graph option with the Git Log command to view the commit history in the form of a graph. This can be helpful if we wish to understand how different branches were created and merged with each other. It is often combined with the --oneline option to better visualize the history.

$ git log --graph

Consider the case where we have three branches in our repository. The following image illustrates the output of the Git Log command with the --graph option.

Output of Git Log with the --graph option

The above output can be a little difficult to understand if you are using the --graph option for the first time. The *(asterisk) denotes a commit and the pipes and slashes(|, \, /) are just used to denote the parents of the commits.

Custom Formatting

We can completely format the output of the Git Log command by using the --pretty option. We have a lot of specifiers that represent various information, like %H stands for commit hash, %cn stands for committer name, etc. Consider the following example where we are using the --pretty option to format the output so that only the commit hash and the committer name are shown.

$ git log --pretty=format:"Commit Hash: %H, Commit Made By: %cn" 

The output of the command is shown in the image below.

Custom formatting using --pretty option

The list of some important specifiers is given below.

Specifier Output
%H Commit hash
%h First seven characters of the commit hash
%an Author name
%ae Author email
%ad Author date
%cn Committer name
%ce Committer email
%cd Committer date
%s The subject of the commit message

Filtering the Commit History

Filtering allows us to view only some part of the history that matches some conditions. Let's learn different options that can be used to filter the commit history.

Filter by Number of Commits

By default, the Git Log command will output all the commits that were made to a certain branch. If we only wish to see some of the most recent commits and not all, then we can use the -n flag followed by the number of commits that we want to see.

$ git log -n2

For example, the above command will output the 2 most recent commits.

Viewing the 2 most recent commits using Git Log command.

Filter by Date

We can use the --before and --after options with the Git Log command to specify a time range and view only the commits that were made in that time period. The following examples explain how to use these options.

To view commits made after a certain date:

$ git log --after="YYYY-MM-DD"

To view commits made before a certain date:

$ git log --before="YYYY-MM-DD"

To view commits made in the last n days:

$ git log --after="n days ago"

To view commits made between two dates:

$ git log --after="YYYY-MM-DD" --before="YYYY-MM-DD"

Filter by Author Name

A branch may be shared between developers and in such cases, the commit history will have multiple authors. We can filter out only those commits made by a certain author by using the --author option.

$ git log --author="author-name"

Filter by Commit Message

We can use the --grep option with the Git Log command to filter out commits that have a matching commit message. It will output all the commits that have the matching words in their commit message.

$ git log --grep="commit-message"

For example, suppose that we need all the commits that had something to do with a file called file2. The commit messages will have the word "file2" in them. The following code shows how to fetch only these commits.

Viewing the output of Git Log with the --grep option

Summary

The Git log command is used to view the history of our project. It is used to view the commit history and other information related to commits like who made the commit, when was the commit made, the commit message, and the commit hash. We can format and filter the output of the Git Log command in a lot of different ways.