7

Is there a simple git command to list files whose diffs contain a given string, or will I have to iterate?

I need to commit a refactoring (method name change) separately from other changes, so I need to identify files whose diffs contain the new method name.

Szczepan Hołyszewski
  • 2,707
  • 2
  • 25
  • 39

1 Answers1

9

You can use the -S (string) or -G (regexp) parameters of the diff command to filter with a specific string.

git diff --name-only -S"your_method_name"

And only files where this specific string appears in the changes will show up in the list.

Take a look here in the doc :

-S<string>

Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion) in a file. Intended for the scripter’s use.

It is useful when you’re looking for an exact block of code (like a struct), and want to know the history of that block since it first came into being: use the feature iteratively to feed the interesting block in the preimage back into -S, and keep going until you get the very first version of the block.

Community
  • 1
  • 1
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • The difference between `-S` and `-G` is not limited to string vs. regex. See [this example](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--Gltregexgt) on the difference between these two options. – Emadpres Sep 28 '21 at 12:54