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

查看:50
本文介绍了如何找到在任何分支中引入了字符串的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\nmsvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)\nfor 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天全站免登陆