Git – Filtering the Commit History
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
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
Print Statistics: We may need to print information about the commit in detail. We will use –stat option.
$ git log --stat
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
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
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
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
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
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
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"
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 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
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