如何在包含特定文本的给定文件中查找提交?

jer*_*rge 9 git

我的 git 存储库中有一个文件被更改了很多次。我想知道此文件中存在特定文本的修订。

有没有 git 命令可以获取这些信息?

就像是:

git find "specific_text" -- /frequently/modified/file
Run Code Online (Sandbox Code Playgroud)

这将输出提交列表。

LeG*_*GEC 11

git grep <pattern>将仅搜索一个提交

如果您想搜索git 的所有历史记录,请使用镐选项之一:

git log -G "specific_text" -- frequently/modified/file
# or :
git log -S "specific_text" -- frequently/modified/file
Run Code Online (Sandbox Code Playgroud)

文档中解释了两者之间的区别:

  • -Gspecific_text将列出差异中出现的所有提交,
  • -S只会列出更改计数的提交specific_text,例如:此文本出现的提交、被删除的提交,但不会列出仅移动它而不添加或删除实例的提交。

-p如果您想查看差异本身,您还可以添加:

git log -p -S "specific_text" -- frequently/modified/file
Run Code Online (Sandbox Code Playgroud)