是否可以在Git项目的所有分支中执行"grep搜索"? [英] Is it possible to perform a 'grep search' in all the branches of a Git project?
问题描述
是否可以在基于Git控件的项目的所有分支中运行git grep
?还是有另一个命令要运行?
Is it possible to run git grep
inside all the branches of a Git control sourced project? Or is there another command to run?
推荐答案
问题"如何在git历史?"建议:
git grep <regexp> $(git rev-list --all)
它将搜索所有提交,其中应包括所有分支.
That searches through all the commits, which should include all the branches.
另一种形式是:
git rev-list --all | (
while read revision; do
git grep -F 'yourWord' $revision
done
)
您可以在中找到更多示例这篇文章:
我在一个足够大的项目上尝试了上述操作,以至于git抱怨参数的大小,因此,如果遇到此问题,请执行以下操作:
I tried the above on one project large enough that git complained about the argument size, so if you run into this problem, do something like:
git rev-list --all | (while read rev; do git grep -e <regexp> $rev; done)
(请参见下面此答案的最后一部分中的替代方法)
(see an alternative in the last section of this answer, below)
如果需要,请不要忘记这些设置:
Don't forget those settings, if you want them:
# Allow Extended Regular Expressions
git config --global grep.extendRegexp true
# Always Include Line Numbers
git config --global grep.lineNumber true
此别名也可以提供帮助:
This alias can help too:
git config --global alias.g "grep --break --heading --line-number"
注意: chernjie
Note: chernjie suggested that git rev-list --all
is an overkill.
更精致的命令可以是:
A more refined command can be:
git branch -a | tr -d \* | xargs git grep <regexp>
仅允许您搜索分支(包括远程分支)
Which will allow you to search only branches (including remote branches)
您甚至可以为其创建bash/zsh别名:
You can even create a bash/zsh alias for it:
alias grep_all="git branch -a | tr -d \* | xargs git grep"
grep_all <regexp>
2016年8月更新: R.M. 在评论中建议
Update August 2016: R.M. recommends in the comments
尝试
git branch
版本时,我得到了"fatal: bad flag '->' used after filename
".该错误与HEAD
别名表示法相关.
I got a "
fatal: bad flag '->' used after filename
" when trying thegit branch
version. The error was associated with aHEAD
aliasing notation.
我通过在tr
和xargs
命令之间的管道中添加sed '/->/d'
来解决它.
I solved it by adding a sed '/->/d'
in the pipe, between the tr
and the xargs
commands.
git branch -a | tr -d \* | sed '/->/d' | xargs git grep <regexp>
也就是说:
alias grep_all="git branch -a | tr -d \* | sed '/->/d' | xargs git grep"
grep_all <regexp>
这篇关于是否可以在Git项目的所有分支中执行"grep搜索"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!