Git 在单个文件的历史记录中搜索字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10215197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:30:22  来源:igfitidea点击:

Git search for string in a single file's history

git

提问by JD Isaacks

So if I have a file called foo.rband it is giving me an error for a missing method called bar, so I want to search the history of foo.rbfor the string barto see if it was ever defined in the past.

因此,如果我有一个名为foo.rb的文件并且它给我一个名为bar的缺失方法的错误,那么我想搜索foo.rb的历史字符串bar以查看它是否在过去定义过。

I found this Search all of Git history for a string?

我发现这个Search all of Git history for a string?

But this searches all files. I just want to search in one file.

但这会搜索所有文件。我只想在一个文件中搜索。

回答by ralphtheninja

For this purpose you can use the -S option to git log:

为此,您可以使用 -S 选项来 git log:

git log -S'bar' -- foo.rb

回答by Antoine Pelisse

Or maybe you can try this one (from related questions Search all of git history for string)

或者你可以试试这个(从相关问题Search all of git history for string

git rev-list --all foo.rb | (
    while read revision; do
        git grep -F 'bar' $revision foo.rb
    done
)

It will actually look for file content and not commit messages/patches for any occurence of bar.

它实际上会查找文件内容,并且不会为 bar 的任何出现提交消息/补丁。

回答by Dmitry Reznik

There's an override for git log command (from manual):

git log 命令有一个覆盖(来自手册):

$ git log Makefile      # commits that modify Makefile

So you could use:

所以你可以使用:

git log foo.rb | grep "bar"

回答by stack questions

I used git log -S "string" --follow -p "path of file"which shows the full history of all changes with that string.

我使用 git log -S "string" --follow -p "path of file"which 显示了该字符串的所有更改的完整历史记录。