寻找在任何分支中引入字符串的Git提交 [英] Finding a Git commit that introduced a string in any branch
问题描述
我希望能够找到某个字符串,这个字符串是在
任何分支中的任何提交中引入的,我该怎么做?我发现了一些(我为Win32修改的),
但是 git whatchanged
似乎没有查看不同的分支
(忽略py3k chunk,它只是一个msys / win的换行修正)
git whatchanged - < file> ; | \
grep^ commit| \
python -cexec(\import sys,msvcrt,os\\\
msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY)\ nfor sys.stdin中的l:print( l.split()[1])\)| \
xargs -i%git show origin% - < file>
无论您的解决方案是否缓慢,都无关紧要。
<您可以这样做:
git log -S - <无论> --source --all
要查找所有添加或删除固定字符串 无论
。 - all
参数表示从每个分支开始, - source
表示显示哪些分支导致发现提交。添加 -p
以显示每个提交所引入的补丁程序通常很有用。
版本git自1.7.4以来也有类似的 -G
选项,它采用正则表达式。这实际上有不同的(更明显的)语义,在 Junio Hamano发布的这篇博文中有解释。
正如 thameera 在评论中指出的那样,您需要如果搜索词包含空格或其他特殊字符,则在引号周围加引号:例如: $ b
git log -S'hello world'--source --all
git log -S - 伙计,我的车在哪里? --source --all
下面是一个使用 -G
找到函数foo(){
:
git log -G^(\s)* function foo [(] [)](\s)* {$--source --all
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.
You can do:
git log -S <whatever> --source --all
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.
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.
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
Here's an example using -G
to find occurrences of function foo() {
:
git log -G "^(\s)*function foo[(][)](\s)*{$" --source --all
这篇关于寻找在任何分支中引入字符串的Git提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!