Search Git History for Certain Words and Phrases


When I started my first job at a company that used git for version control, I had never worked on projects of a scale that required more than a rudimentary knowledge of how to use git (I could add, commit, push, and pull, without really understanding what was happening the whole time). Years later, I use a nuanced and broad range of git commands regularly, even for personal projects.

To date, my favorite command (spoiler: THE ONE I'M ABOUT TO TELL YOU ABOUT) helps search my history for given words or phrases.

Find Commit Messages Matching Your Search Term

If you want to find all commits where the commit message contains a given word, you can take advantage of grep:

git log --grep=SOME_WORD

# OR WITH reflog:

git reflog --grep-reflog=SOME_WORD

For example, say I run:

git log --grep=fix

This should output what looks like a normal git log except that only commit messages with some semblance of the word "style" show up.

Git log

Searching for Multiple Words or a Phrase

If you want to search for multiple words or a phrase, you just have to use quotes:

git log --grep='my phrase'

# SAME WITH --grep-reflog

git reflog --grep-reflog='my phrase'

I hope this was post was helpful! Thanks for taking the time read it!