关于git:列出其更改(diff)包含给定字符串的文件?

List files whose changes (diff) contain a given string?

本问题已经有最佳答案,请猛点这里访问。

是否有一个简单的git命令来列出其差异包含给定字符串的文件,或者我必须迭代?

我需要与其他更改分开提交重构(方法名称更改),因此我需要识别其diff包含新方法名称的文件。


您可以使用diff命令的-S(字符串)或-G(regexp)参数来过滤特定字符串。

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

并且只有更改中出现此特定字符串的文件才会显示在列表中。

在文档中查看:

-S

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.