0

Is there a way to make git grep only match files that I have committed?

The purpose is to grep in the contents of the file, not the log message.

EquipDev
  • 5,573
  • 10
  • 37
  • 63
  • Define "files that I have committed": Files that no one else ever modified? Files modified anytime by one of your commits? Files where the last commit touching them is by you? Something else? – Michał Politowski Sep 20 '17 at 10:25
  • Condition as for `git log --committer=` does just fine. – EquipDev Sep 20 '17 at 11:21

2 Answers2

3
author="your name"

git log --pretty="%H" --author="$author" | while read commit_hash; do git show --oneline --name-only $commit_hash | tail -n+2; done | sort | uniq |xargs grep -i "String to grep"

OR shorter version:

author="your name"
git log --no-merges --author="$author" --name-only --pretty=format:"" | sort -u |xargs grep -i <string>

Orignal answer used to get the files from a particular user

P....
  • 17,421
  • 2
  • 32
  • 52
  • The purpose was to grep in the contents of the file, not the log message. I have update the question to clarify this. – EquipDev Sep 20 '17 at 09:57
  • @EquipDev this will indeed grep from the contents of the files commited by the author not the log. – P.... Sep 20 '17 at 10:00
3

If you want to grep in the diff of a commit, you should probably use git log -G or git log -S :

git log -p --author="your name" -G pattern

Both -G and -S will look for the pattern in the diff introduced by a commit,
the difference is (git help log) :

To illustrate the difference between -S<regex> --pickaxe-regex and -G<regex>, consider a commit with the following diff in the same file:

+    return !regexec(regexp, two->ptr, 1, &regmatch, 0);
...
-    hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);

While git log -G"regexec\(regexp" will show this commit, git log -S"regexec\(regexp" --pickaxe-regex will not (because the number of occurrences of that string did not change).

When using -p in conjunction with -G or -S, only the files matching the pattern will be displayed.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • The purpose was to grep in the contents of the file, not the log message. I have update the question to clarify this. – EquipDev Sep 20 '17 at 09:57
  • @EquipDev : that's exactly what `-G` and `-S` do : they grep in the *content* of the commit, not in its message – LeGEC Sep 20 '17 at 09:59
  • Thanks, I was not aware for -G and -S; still learning Git :-) Great that the git command can do that with options, instead of having to add pipes, since it thereby also works on Windows. – EquipDev Sep 20 '17 at 11:21