如何找到在任何分支中引入字符串的Git提交?

我希望能够找到任何分支中任何提交中引入的某个字符串,我该怎么做?我找到了一些东西(我为Win32修改过),但git whatchanged似乎没有查看不同的分支(忽略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>

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


犯罪嫌疑人X
浏览 522回答 3
3回答

翻阅古今

你可以做:git log -S <whatever> --source --all查找添加或删除固定字符串的 所有提交whatever。该--all参数意味着从每个分支开始,并且--source意味着显示哪些分支导致找到该提交。添加-p以显示每个提交也将引入的补丁通常很有用。自1.7.4以来的git版本也有类似的-G选项,它采用正则表达式。这实际上有不同(而且更明显)的语义,在Junio Hamano的博客文章中有所解释。正如thameera在评论中指出的那样,如果它包含空格或其他特殊字符,则需要在搜索词周围加上引号,例如:git log -S 'hello world' --source --allgit log -S "dude, where's my car?" --source --all这是一个-G用于查找出现次数的示例function foo() {:git log -G "^(\s)*function foo[(][)](\s)*{$" --source --all

眼眸繁星

--reverse也很有帮助,因为您需要进行更改的第一个提交:git log --all -p --reverse --source -S 'needle'这样,旧的提交将首先出现。

慕慕森

用相同的答案搞清楚:$ git config --global alias.find '!git log --color -p -S '!因为其他方式,git不能正确地将参数传递给-S。看到这个回复--color和-p有助于准确显示“whatchanged”现在你可以做到$ git find <whatever>要么$ git find <whatever> --all$ git find <whatever> master develop
打开App,查看更多内容
随时随地看视频慕课网APP