我希望能够找到在任何提交中引入的某个字符串 任何分支,我怎么做呢?我发现了一些东西(我为Win32修改了), 但是git所做的改变似乎并没有深入到不同的分支 (忽略py3k块,这只是一个msys/win换行修复)

git whatchanged -- <file> | \
grep "^commit " | \
python -c "exec(\"import sys,msvcrt,os\nmsvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)\nfor l in sys.stdin: print(l.split()[1])\")" | \
xargs -i% git show origin % -- <file>

如果你的解决方案很慢,这并不重要。


当前回答

git log -S"string_to_search" # options like --source --reverse --all etc

注意不要在“S”和“string_to_search”之间使用空格。在一些设置中(git 1.7.1),你会得到这样的错误:

fatal: ambiguous argument 'string_to_search': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

其他回答

用同样的答案瞎折腾:

$ git config --global alias.find '!git log --color -p -S '

! 是必需的,因为其他方式,git不会正确地将参数传递给-S。看看这个回应 ——color和-p有助于准确显示“发生了什么变化”

现在你可以

$ git find <whatever>

or

$ git find <whatever> --all
$ git find <whatever> master develop
git log -S"string_to_search" # options like --source --reverse --all etc

注意不要在“S”和“string_to_search”之间使用空格。在一些设置中(git 1.7.1),你会得到这样的错误:

fatal: ambiguous argument 'string_to_search': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

Mark Longair的回答很好,但我发现这个更简单的版本对我很有用。

git log -S whatever

虽然这并没有直接回答你的问题,但我认为这可能是你未来的一个很好的解决方案。我看到了我的一部分代码,很糟糕。不知道是谁写的,什么时候写的。我可以看到文件中的所有更改,但很明显,代码已经从其他文件移到了这个文件中。我想知道到底是谁先把它加进去的。

要做到这一点,我使用Git平分,这很快让我找到罪人。

我运行git bisect start,然后git bisect bad,因为修订检查出来有这个问题。因为我不知道问题是什么时候发生的,我把第一次提交的目标定为“good”,git平分good <initial sha>。

然后我就一直在回购中寻找坏代码。当我找到它时,我运行git bisect bad,当它不存在时:git bisect good。

在大约11个步骤中,我已经讨论了大约1000个提交,并找到了引入问题的确切提交。好极了。

——reverse也很有用,因为你想要的是做出更改的第一个提交:

git log --all -p --reverse --source -S 'needle'

这样旧的提交将首先出现。