Find Git Commits by Commit Message Text

TIL that you can reference git commits by providing (parts of) the commit message

Today I learned: When using the git CLI you can reference a commit by its commit message using the :/some text syntax.


When working with the git CLI you sometimes need to reference a specific commit (or rather revision).

A few common examples:

I was aware of two common ways to provide that <rev> parameter:

  1. Using the SHA-1 name of the commit (either in the full form or a short form): git show dae86e.
  2. Using the position relative to the HEAD: git show HEAD~3 shows the changes of the commit 3 revisions prior to the latest commit.

Looking up the commit SHA or the relative position requires me to go back and forth between git log and the command that I actually wanted to run. It works, but it’s tedious and commit SHAs are rather hard to memorize.

Turns out there’s another way I wasn’t aware of: You can find a specific git commit using a regular expression that matches the commit message. To do so, you use the :/<text> syntax for the revision.

A simple git show :/fix typo would identify the most recent commit whose commit message matches “fix typo”. In some cases this can be simpler than providing a SHA or a relative position.

Turns out that reading the documentation of a tool you use dozens of times a day does come in handy. Who would’ve thought?