git,

About filtering Git commits (CHEATSHEET)

Oct 19, 2022 · 2 mins read · Post a comment

Filtering commits is yet another handy feature about Git. You can filter by author, by date, commit ID (SHA1 hash), commit message, and few other things I suppose. My goal in this post is to try to go over each of them.

Prerequisites

  • Git

Solution

By author

List all commits grouped by author’s name:

git shortlog

List commits by certain author:

git log --author='John Smith'

List commits by multiple authors:

git log --author='John Smith' --author='Linuz Torwalds'

Filter your own commits:

git log --author=$(git config user.email)

Exclude commits by author’s name:

git log --format='%H %an' | 
  grep -v John | 
  cut -d ' ' -f1 | 
  xargs -n1 git log -1

Note(s):

  • Quotes are optional if you don’t need any spaces for instance filtering by author’s name only (not surname).
  • Adding the -all option will filter across all branches, not just the current commit’s ancestors.

By date

List commits in a specific data range:

git log --since='1 week ago'

or,

git log --since='September 16 2021' --until 'September 31 2022'

By commit ID

Search for commit by commit ID:

git show <commit-SHA1>

If you don’t want to show the diff, run:

git show <commit-SHA1> --no-patch

List all commits between two commit IDs:

git log <commit1-SHA1>...<commit2-SHA1>

or, if you want to get all commits from a specific one up to the latest, run:

git log <commit-SHA1>...HEAD

By commit message

List all commits that includes particular word. For instance:

git log --all --grep='Bug fixing'

Other

List the last 5 commits:

git log -5

List all commits related to a single file:

git log index.html

Conclusion

List of related posts:

To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git