1

How can I refer to a git commit by the text / words in its commit message headline?

I want to avoid:

  • Mouse copy / paste of a commit hash from git log
  • Typing of commit hexadecimal digits
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • 1
    Git commits don't have a "name", but the closest thing they have to one would be their hash or possibly a ref like a branch or tag. – ChrisGPT was on strike Aug 27 '18 at 12:40
  • Please specify _"name"_. Do you mean its commit message head line? – Clijsters Aug 27 '18 at 12:50
  • 1
    The hash ID of a commit *is* its name. All other strings are merely indirect ways to name the hash ID. That said, `:/string` as in Tom Hale's answer is usually the easiest short-cut. See [the gitrevisions documentation](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html) for many other ways to name a commit. – torek Aug 27 '18 at 15:31

1 Answers1

5

TL;DR:

Reference the most recent commit that matches <text>:

:/<text>, e.g. :/fix nasty bug

Reference the most recent commit reachable from <rev> that matches <text>

   <rev>^{/<text>}, e.g. HEAD^{/fix nasty bug}

Manual page entries

man gitrevisions says:

Reference the most recent commit reachable from <rev> that matches <text> :

   <rev>^{/<text>}, e.g. HEAD^{/fix nasty bug}
       A suffix ^ to a revision parameter, followed by a brace pair that
       contains a text led by a slash, is the same as the :/fix nasty bug
       syntax below except that it returns the youngest matching commit
       which is reachable from the <rev> before ^.

Reference the most recent commit anywhere that matches <text> :

   :/<text>, e.g. :/fix nasty bug
       A colon, followed by a slash, followed by a text, names a commit
       whose commit message matches the specified regular expression. This
       name returns the youngest matching commit which is reachable from any
       ref. The regular expression can match any part of the commit message.
       To match messages starting with a string, one can use e.g.  :/^foo.
       The special sequence :/!  is reserved for modifiers to what is
       matched.  :/!-foo performs a negative match, while :/!!foo matches a
       literal !  character, followed by foo. Any other sequence beginning
       with :/!  is reserved for now. Depending on the given text, the
       shell’s word splitting rules might require additional quoting.
Community
  • 1
  • 1
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • 1
    Be careful with this—there's no guarantee that commit messages are in any way unique. – ChrisGPT was on strike Aug 27 '18 at 12:40
  • Yes, it will reference the most recent match. – Tom Hale Aug 28 '18 at 05:06
  • This doesn't seem to work if your commit message has backticks in it. Ex: ```git log -1 "HEAD^{/feat: Update `my_func`.}"```. Outputs: ```fatal: ambiguous argument 'HEAD^{/feat: Update `my_func`.}': unknown revision or path not in the working tree.``` (while the commit with this name actually exists and should be returned). – LoneCodeRanger Mar 15 '23 at 16:23