Is there any way to get the SHA of a commit from its message?

前端 未结 3 2384
一整个雨季
一整个雨季 2021-02-19 23:19

When doing a git tag, I\'m not always great at remembering if HEAD~6 (for example) is inclusive or exclusive.

Given that most of my commits are prefixed wi

3条回答
  •  北海茫月
    2021-02-20 00:06

    git log --grep=word
    

    should help find the relevant commit.
    (as opposed to searching for commit contents, where git log -S (pickaxe option) or git grep are more appropriate)

    Add various format options like

    git log --grep=word --pretty=oneline
    git log --grep=word --pretty=format:"%h"
    

    (the last one displaying only the SHA1), and you are good to go.


    See also "Fun with git log --grep"

    git log --grep=frotz --grep=nitfol --since=1.month
    

    This finds the commits that happened within the last month and mentioned either frotz or nitfol in their commit log messages.
    As with the usual "git grep", giving more than one patterns means "this or that".

    The article points out that git grep is line oriented, and use the --all-match option in this git log --grep command.

    The way "git log" family uses the grep mechanism is exactly for this "does the whole thing has a match?" aspect , not "show me the lines that match these criteria" aspect. It allows you to use this mechanism like so:

    git log --all-match --grep=frotz --author=Linus
    

    This will show commits that mention frotz and written by Linus.

提交回复
热议问题