Git - Filtering the Commit History
Last Updated : 24 Mar, 2022
Git source code versioning tool provides a lot of features. One of the most important and useful features is log or history. We can use git log command in order to list, filter, view commit history in different ways. Here we will examine git log command usage in detail with examples.
List Commit History: We will start with git log command without any parameter. This will list all commit history in an interactive terminal where we can see and navigate.
$ git log
List Commit History We can see from the output the following information about the commit provided.
- Commit number which is a unique hash identifies the commit
- Author the developer who commits. Also, email information is provided
- Date specifies when the commit occurred
- The last line provides notes and information about the commit.
List One Commit Per Line: If we need to only list the unique part of the commit id with the note provided by an author we can use --oneline option which will just print a single line about each commit.
$ git log --oneline
List One Commit Per Line Print Statistics: We may need to print information about the commit in detail. We will use --stat option.
$ git log --stat
Print Statistics We can see from the output that extra information like changed file, changed file count, number of lines added, number of lines deleted.
Print Patch or Diff Information: If we are interested in the code diff information we need to use -p option. -p option can be used to print path or diff of the files for the commits.
$ git log -p
Print Patch or Diff Information We see from the screenshot that added and removed code is shown clearly. Added code color is green and the removed code is red. Also added code lines start with +plus and removed code lines start with - minus.
Show/Print Specific Commit In Detail: If we need to look at a specific commit we need to use the git show command. We will also provide the commit id or number we can print.
$ git show b1efd742499b00eef970feeef84dc64f301db61f
Print Specific Commit In Detail We can see that specific commit provides diff information in detail.
Show/Print Specific Commit Stats: If we cant to just print specific commit stat and information we can provide --stat option to the git show command.
$ git show --stat b1efd742499b00eef970feeef84dc64f301db61f
Show/Print Specific Commit Stats Group Commits By Author: If we want to inspect the commits according to the author's name we need to group commits by author. We can use shortlog command in order to list commits notes grouped by author name.
$ git shortlog
Group Commits By Author Show Author Commit Numbers: If we are interested in the author's commit numbers we need to provide -s options to the shortlog command. This will provide commit numbers for each author.
$ git shortlog -s
Show Author Commit Numbers Sort Authors By Commit Numbers: We can improve previous examples and sort authors by their commit numbers. We will add -n too the previous example where the final command will be like below.
$ git shortlog -n -s
Sort Authors By Commit Numbers Pretty Print: We can also customize the log output according to our needs. We can use --pretty option and some parameters to print different log output. Here in this example, we will use %cn for author name %h hash value of commit and %cd for commit time.
$ git log --pretty="%cn committed %h on %cd"
Pretty Print Filter By Author: In some cases, we may need to filter commits according to the author name. We will use --author and provide the author name to filter and show only given author. In this example, we will filter the author named dmiller.
$ git log --author="dmiller"
Filter by Author Filter By Number: If we want to list and print the specified number of commits we need to use - with the number we want to print. In this example, we will print the last 5 commits.
$ git log -5 --oneline
Filter by Number List Only Merges: By default, merge commits are printed and listed. But if the default behavior is changed with config and we want to list and print merge commits we can use --merge option to list merge commits too.
$ git log --merge
List No Merges: By default merges commits are printed and listed with the git log command. If we do not want to list or print then for all operations we can use the --no-merges option which will do not show merge commits.
$ git log --no-merge
Similar Reads
Remove Large Files from Commit History in Git Sometimes, you may accidentally commit a large file to your Git repository, making it unwieldy and slow. Large files can significantly increase the size of your repository, making operations like cloning, fetching, and pulling slower and more cumbersome. This can be particularly problematic in team
2 min read
How to Delete Last Commit in Git? Sometimes, you may want to delete your last commit because of mistakes, accidentally adding sensitive information, or just wanting to change your commit history. Here are the three ways to delete the last commit in Git-1. Delete the Last Commit and Keep the ChangesThis method removes the last commit
4 min read
Committing in Git Committing changes is a fundamental aspect of using Git. The commit process involves recording snapshots of your project at various stages, allowing you to track progress, revert changes, and collaborate with others efficiently. In this article, we'll explore the complexity of committing in Git, fro
7 min read
How to Remove a Large File from Commit History in Git? Simply removing a large file through a commit won't truly eliminate it. Git stores all versions of files in its history, even deleted ones, to allow for easy recovery. This can lead to wasted disk space. We will discuss the various approaches for removing a large file from the commit history in Git:
4 min read
How To Get The Git Commit Count? Tracking the number of commits in your Git repository can provide insights into the development progress, team activity, and overall project health. Whether you want to get the total commit count or see the number of commits by each contributor, Git offers several ways to retrieve this information.
2 min read