1

If I search for all changes which contain "foo" with git log -G foo I get a list of commits.

Now these commits can have a lot of changes which don't contain "foo".

Is there a way to show only the changes/diffs which contain "foo"?

I could help myself with a ugly shell script and "grep -A ... -B ..." but maybe there is an easier solution?

Third party tools are valid answers, too.

guettli
  • 25,042
  • 81
  • 346
  • 663
  • This looks like a duplicate of https://stackoverflow.com/q/51596369/. Check if my answer there helps: https://stackoverflow.com/a/64616432/. [My Gist](https://gist.github.com/phil-blain/2a1cf81a0030001d33158e44a35ceda6) has a little bit better version of the script. – philb Aug 23 '22 at 12:34

2 Answers2

1

The -G <pattern> option affects the output of the -p, --name-only and --name-status options, which will only show files whose diff contain <pattern>.

So git log -G foo -p already filters out irrelevant files.

The above command will show the complete diff for each file, however, so you will still need some external processing to isolate the chunks within the diff, and output only the chunks which contain foo.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

You could filter the full outputs of these commits' diffs via a simple grep

git log -p -G foo | grep foo

It will only pick the lines with said string, though, without their original diff context.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61