Skip to content
shellhacks.dev
Command Line

Search Git Commit Messages with git log --grep

Search commit messages with git log --grep=pattern. Matching is case sensitive until you add the ignore-case option, two patterns are ORed, and --all-match requires both.

2 min read
Summarize in AI

You remember a word from a commit message and want the commits that mention it.

Shell
git init -q repo && cd repo
git config user.name Ann && git config user.email [email protected]
for m in 'fix: crash on empty input' 'feat: add export' 'Fix typo in README' 'fix typo in export header'; do
  git commit -q --allow-empty -m "$m"
done
echo "--- case sensitive"; git log --oneline --grep=fix
echo "--- ignore case"; git log --oneline -i --grep=fix
echo "--- two patterns are OR"; git log --oneline --grep=crash --grep=export
echo "--- all must match"; git log --oneline --all-match --grep=fix --grep=export
Text
--- case sensitive
aa66a22 fix typo in export header
7730e14 fix: crash on empty input
--- ignore case
aa66a22 fix typo in export header
dda1a28 Fix typo in README
7730e14 fix: crash on empty input
--- two patterns are OR
aa66a22 fix typo in export header
d029639 feat: add export
7730e14 fix: crash on empty input
--- all must match
aa66a22 fix typo in export header

--grep takes a regular expression and shows the commits whose message matches. The first run matched only the two commits with a lowercase fix, and -i added Fix typo in README. Giving --grep twice is an OR, so crash or export found three commits, and --all-match turns the list into an AND, leaving the one commit that has both words. See git log.

The pattern is a basic regular expression by default, and -E switches to extended syntax. It searches the commit message only. Use -S or -G to search the changes, and --author for the person. With --all-match the patterns apply to the whole message, not to one line.

Taggedgit
Share

More snippets

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.