如何找到在任何分支中引入字符串的 Git 提交? [英] How to find the Git commit that introduced a string in any branch?

查看:35
本文介绍了如何找到在任何分支中引入字符串的 Git 提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to find a certain string which was introduced in any commit in any branch, how can I do that? I found something (that I modified for Win32), but git whatchanged doesn't seem to be looking into the different branches (ignore the py3k chunk, it's just a msys/win line feed fix)

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

您的解决方案是否缓慢并不重要.

It doesn't really matter if your solution is slow.

推荐答案

您可以:

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

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

To find all commits that added or removed the fixed string whatever. The --all parameter means to start from every branch and --source means to show which of those branches led to finding that commit. It's often useful to add -p to show the patches that each of those commits would introduce as well.

1.7.4 之后的 git 版本也有类似的 -G 选项,它采用正则表达式.这实际上具有不同(而且更明显)的语义,在 这篇来自 Junio Hamano 的博文 中进行了解释.

Versions of git since 1.7.4 also have a similar -G option, which takes a regular expression. This actually has different (and rather more obvious) semantics, explained in this blog post from Junio Hamano.

正如 thameera 在评论中指出的那样,如果搜索词包含空格或其他特殊字符,例如:

As thameera points out in the comments, you need to put quotes around the search term if it contains spaces or other special characters, for example:

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

这是一个使用 -G 来查找 function foo() {:

Here's an example using -G to find occurrences of function foo() {:

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

这篇关于如何找到在任何分支中引入字符串的 Git 提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆