r/git 7d ago

Find Git Commits by Commit Message Text

https://hamvocke.com/blog/git-revisions-by-text/
20 Upvotes

8 comments sorted by

43

u/WoodyTheWorker 6d ago

--grep pattern

7

u/Oakchris1955 6d ago

Yeah, that is literally in the Git book.

10

u/Broad-Promise6954 ancient 6d ago edited 6d ago

One-line summary of blog post, :/text as a revision specifier as mentioned in gitrevisions.

There's an important caveat (as in "caveat emptor", buyer beware) that's not at all obvious to many Git users:

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, including HEAD ...

(the boldface in the quoted text here is mine, to emphasize the caveat I'm talking about). If you have a lot of branch and tag names, the revision you select with :/text may not be the one you expect. You may wish to use the more explicit rev^{/text} form, which searches from the given revision.

2

u/ppww 6d ago

Yes, it's really unfortunate that :/<text> searches all reachable commits, rather than just those that are reachable from HEAD

6

u/RevRagnarok 6d ago

5

u/Orlandocollins 5d ago

Oh I connect with this sooo much. I am so tired of seeing horrible commit messages that are just a squash message of someones dozen or more commits that was generated when they click the squash and merge button in githubs UI and then accept without doing any sort of cleanup. Drives me up the wall

3

u/Beautiful-Energy2169 6d ago

Neither --grep nor :/text sees commits that are no longer reachable. On a scratch repo just now, after git reset --hard dropped two commits, git log --grep and git log --all --grep returned nothing and :/text errored as an unknown revision, while git log -g --grep=... found it in the reflog straight away.

1

u/haywire 6d ago

What?