How to Use Git Log to Format the Commit History?

The commit history in a Git repository serves as a timeline of changes, documenting the evolution of a project over time. While Git’s default commit history display provides important information, customizing the format of the commit log can offer deeper insights and improve readability.

Git’s powerful git log command allows you to use the presentation of commit history to suit your preferences and requirements. In this article, we’ll explore how to use git log to format the commit history effectively, enabling you to gain better visibility into project changes and streamline collaboration.

What is Git Log?

git log command is used to view the list of commit logs. The data is represented in a reverse log manner, i.e., showing the recent ones at the top and others at the bottom. The log contains details about the timestamp of the commit, the author of the commit, the branch on which the commit was made, the SHA(Secure Hash Algorithm) ID of the commit, and the commit message.

Formatting Commit History

Git provides a range of options to customize the format of the commit history displayed by git log. These options allow you to specify which commit metadata to include, how it should be formatted, and even add custom elements to the output.

The various options to format the git log are as follows: 

--author=<Name>

It helps to format the log for those commits only where the author was a specific user. This option helps to review any logs made by any specific user overtime on a repository.

--since=<Date> / --after=<Date>

These options help to format the log based on the timestamps and query only those commit logs which fulfill the criteria.

-n <number>

The -n option helps to limit the log commits view, i.e. only a certain number of recent commits will be displayed instead of showing a bulk of updates.

--grep=<pattern>

The –grep options help the user to search for a particular pattern or word in the whole commit history and display on those commits which consist of the pattern. This option is useful when a user is looking for updates related to a specific file or object.

--graph

The –graph option displays the updates in a graphical format, displaying the branching and the merging commits as diverging and converging graph nodes. Each commit is actually a graph node in the log graph.

--oneline

The – -oneline option is used to format the commit log in as compact as much possible way.

--all

The –all option is used to view the commit log of all branches in a single log.

Note: Although, there are various options to format the log messages. The most useful options are the above-listed ones.