为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Git
犯罪嫌疑人X 2019-09-18 10:57:21
我希望能够找到任何分支中任何提交中引入的某个字符串,我该怎么做?我找到了一些东西(我为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>如果您的解决方案很慢,这并不重要。
查看完整描述

3 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

你可以做:


git log -S <whatever> --source --all

查找添加或删除固定字符串的 所有提交whatever。该--all参数意味着从每个分支开始,并且--source意味着显示哪些分支导致找到该提交。添加-p以显示每个提交也将引入的补丁通常很有用。


自1.7.4以来的git版本也有类似的-G选项,它采用正则表达式。这实际上有不同(而且更明显)的语义,在Junio Hamano的博客文章中有所解释。


正如thameera在评论中指出的那样,如果它包含空格或其他特殊字符,则需要在搜索词周围加上引号,例如:


git log -S 'hello world' --source --all

git log -S "dude, where's my car?" --source --all

这是一个-G用于查找出现次数的示例function foo() {:


git log -G "^(\s)*function foo[(][)](\s)*{$" --source --all


查看完整回答
反对 回复 2019-09-18
?
眼眸繁星

TA贡献1873条经验 获得超9个赞

--reverse也很有帮助,因为您需要进行更改的第一个提交:


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

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


查看完整回答
反对 回复 2019-09-18
?
慕慕森

TA贡献1856条经验 获得超17个赞

用相同的答案搞清楚:


$ 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


查看完整回答
反对 回复 2019-09-18
  • 3 回答
  • 0 关注
  • 524 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信