0

How to grep in commits of specific author(s) with git (i.e., w/o piping to grep)?

I could use git log and pipe into grep, but maybe there's a better (faster?) way?

git log --author="John Doe <jdoe@host>" -p | grep "some interesting stuff"
3
  • 4
    You don’t use git-grep(1) on the log. You use git log --grep=<something> --author=<something> Commented Nov 27, 2024 at 15:06
  • If you mean grep through the diff: git log ... -S for fixed strings and git log ... -G for a regex. Commented Nov 27, 2024 at 18:18
  • thanks! I realize I had a false premise here, and re-phrased the question to not ask specifically about git-grep. What you suggest is exactly what I want, thanks! Commented Nov 29, 2024 at 10:46

1 Answer 1

3

As a @Guildenstern says, you don't need to use git grep, you just need to add --grep flag to your git log command, like this:

git log --author=<something> --grep=<something> 

This grep flag will search only within commit messages. If you need to search in the diff (e.g., some string that was added or removed in the change) you can use the -S flag, like this:

git log --author=<something> -S'changes'

And if you need to search by regex you can use -G flag, like this:

git log --author=<something> -G'^changes'

This will find any changes in the diff that contain a line starting with changes.

For more information about the differences between the -S and -G flags you can check here (thanks @j6t): https://git-scm.com/docs/git-log#Documentation/git-log.txt--Gltregexgt

A few more interesting examples

Find commits from multiple authors

git log --author=<something> --author=<someone>

Use multiple grep flag:

git log --author=<something> --grep=fix --grep=feat

If you need to find a commit that contains both words in a single commit message, add the --all-match flag, like this:

git log --author=<something> --grep=fix --grep=feat --all-match
Sign up to request clarification or add additional context in comments.

2 Comments

The difference between -S and -G is much more fundamental than just fixed string vs. regular expression.
super, thank you! updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.