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:
git show <rev>shows what was changed in a specific commitgit revert <rev>reverts the changes from a specific commit using a new commitgit commit --fixup <rev>to create a fixup commit for the given revision
I was aware of two common ways to provide that <rev> parameter:
- Using the SHA-1 name of the commit (either in the full form or a short form):
git show dae86e. - Using the position relative to the
HEAD:git show HEAD~3shows 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?