Git Log to format the commit history
What is Git Log?
Git log is a command in Git that displays the commit history of a Git repository. It shows the list of commits made in the repository, along with metadata such as the commit author, date, and message.
What is Commit History?
The commit history, on the other hand, is a record of all the changes made to a Git repository. Each commit represents a snapshot of the repository at a specific point in time, along with a message that describes the changes made in that snapshot. The commit history can be used to track the evolution of a project over time, understand the reasoning behind changes made to the code, and collaborate with other contributors to the project. It is an essential tool for managing and versioning code in a Git repository.
How to use Git Log to Format the Commit History?
Git is a popular version control system used for tracking changes in software projects. The git log
command is a powerful tool for examining the commit history of a Git repository. By default, git log
displays a simple list of commits, sorted by date, with each commit’s hash, author, date, and commit message. However, git log
also provides a wide range of options for formatting and filtering the commit history.
Here are some examples of how to use git log
to format the commit history:
Basic log output:
To display the commit history in its default format, simply run the git log
command with no additional options:
sql $ git log commit 08f6b05f7a80a6cfa90d30b947f82cfe8fda2d84 Author: John Doe <[email protected]>Date: Fri Feb 25 10:11:36 2023 -0800Add new feature commit 1e8c10a1b85d146155b6c620e8f9d8539e22f84c Author: Jane Smith <[email protected]>Date: Thu Feb 24 15:22:11 2023 -0800 Fix bu
Display the commit history in a custom format:
To display the commit history in a custom format, use the --format
option followed by a format string. For example, to display each commit’s hash, author, date, and subject line in a custom format, run:
perl $ git log --format='%h %an %ad %s' 08f6b05 John Doe Fri Feb 25 10:11:36 2023 -0800 Add new feature 1e8c10a Jane Smith Thu Feb 24 15:22:11 2023 -0800 Fix bug
- The format string can include placeholders for various commit metadata, such as
%h
for the abbreviated commit hash,%an
for the author name,%ad
for the author date, and%s
for the subject line.
Filter the commit history by author:
To display only the commits made by a specific author, use the --author
option followed by the author’s name or email address. For example, to display only the commits made by John Doe, run:
sql $ git log --author='John Doe'commit 08f6b05f7a80a6cfa90d30b947f82cfe8fda2d84 Author: John Doe <[email protected]>Date: Fri Feb 25 10:11:36 2023 -0800Add new feature
Display the commit history in a graph format:
To display the commit history in a graph format, showing the branching and merging of the repository, use the --graph
option. For example, to display the commit history in a graph format, run:
sql $ git log --graph* commit 08f6b05f7a80a6cfa90d30b947f82cfe8fda2d84 | Author: John Doe <[email protected]>| Date: Fri Feb 25 10:11:36 2023 -0800|| Add new feature |* commit 1e8c10a1b85d146155
Display the commit history in a graph format (continued):
The --graph
option displays a ASCII graph of the commit history, showing the relationships between branches and merges. The --oneline
option can be combined with --graph
to display a simplified version of the graph with only one line of output per commit:
sql $ git log --graph --oneline* 08f6b05 Add new feature * 1e8c10a Fix bug
Display the commit history for a specific file:
To display the commit history for a specific file, use the --follow
option followed by the path to the file. For example, to display the commit history for the README.ME file, run:
sql $ git log --follow README.mdcommit 08f6b05f7a80a6cfa90d30b947f82cfe8fda2d84 Author: John Doe <[email protected]>Date: Fri Feb 25 10:11:36 2023 -0800Add new feature commit 1e8c10a1b85d146155b6c620e8f9d8539e22f84c Author: Jane Smith <[email protected]>Date: Thu Feb 24 15:22:11 2023 -0800 Fix bug commit c67e7d8b76c6545be5e078a2d9977e8c9e3e7b20 Author: John Doe <[email protected]>Date: Wed Feb 23 09:17:44 2023 -0800UpdateREADME.
Display the commit history for a specific branch:
To display the commit history for a specific branch, use the branch name as an argument to the git log
command. For example, to display the commit history for the feature-branch
branch, run:
sql $ git log feature-branch commit 08f6b05f7a80a6cfa90d30b947f82cfe8fda2d84 Author: John Doe <[email protected]>Date: Fri Feb 25 10:11:36 2023 -0800Add new feature commit c67e7d8b76c6545be5e078a2d9977e8c9e3e7b20 Author: John Doe <[email protected]>Date: Wed Feb 23 09:17:44 2023 -0800UpdateREADME.md
These are just a few examples of how to use git log
to format and filter the commit history of a Git repository. The git log
command provides many more options and can be customized to meet the specific needs of your project.
Conclusion:
In conclusion, git log
is a powerful command in Git that can be used to view the commit history of a repository. With its various options, git log
can be used to format and filter the commit history in many different ways, such as displaying the commit history for a specific file or branch, showing a simplified graph of the commit history, or filtering the commits by author or date. By mastering git log
, you can gain a better understanding of the changes made to your repository and improve your ability to collaborate with others on a project.