是否有可能在git项目的所有分支中执行'grep搜索' [英] Is it possible to perform a 'grep search' in all the branches of git project
问题描述
是否可以在git控件源项目的所有分支中运行git grep?还是有另一个命令来运行?
git grep< regexp> $(git rev-list --all)
通过所有提交进行搜索,其中应该包含所有提交
另一种形式是:
git rev-列表--all | (
读取修订版;
git grep -F'yourWord'$ revision
完成
)
你可以在这篇文章:
关于参数大小,所以如果遇到这个问题,可以这样做:
git rev-list --all | (while read rev; do git grep -e< regexp> $ rev; done)
这个答案的最后一部分的替代方案如下)
不要忘记这些设置,如果你想要的话:
#允许扩展正则表达式
git config --global grep.extendRegexp true
#总是包含行号
git config --global grep .lineNumber true
这个别名也可以提供帮助:
git config --global alias.ggrep --break --heading --line-number
更精确的命令可以是:
git branch -a | tr -d \ * | xargs git grep< regexp>
这将允许您仅搜索分支(包括远程分支)
你甚至可以为它创建一个bash / zsh别名:
alias grep_all =git branch -a | tr -d \ * | xargs git grep
grep_all< regexp>
2016年8月更新: RM 在评论中推荐
我得到了一个
致命错误标志 - >'在尝试
之后使用。该错误与git分支
版本时在文件名HEAD
别名符号相关联。
我通过在管道中添加
sed'/ - > / d'
code> tr 和xargs
命令。
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>
Is it possible to run git grep inside all the branches of a git control sourced project ? Or is there another command to run ?
The question "How to grep (search) committed code in the git history?" recommends:
git grep <regexp> $(git rev-list --all)
That searches through all the commits, which should include all the branches.
Another form would be:
git rev-list --all | (
while read revision; do
git grep -F 'yourWord' $revision
done
)
You can find even more example in this article:
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"
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)
You can even create a bash/zsh alias for it:
alias grep_all="git branch -a | tr -d \* | xargs git grep"
grep_all <regexp>
Update August 2016: R.M. recommends in the comments
I got a "
fatal: bad flag '->' used after filename
" when trying thegit branch
version. The error was associated with aHEAD
aliasing notation.I solved it by adding a
sed '/->/d'
in the pipe, between thetr
and thexargs
commands.
git branch -a | tr -d \* | sed '/->/d' | xargs git grep <regexp>
That is:
alias grep_all="git branch -a | tr -d \* | sed '/->/d' | xargs git grep"
grep_all <regexp>
这篇关于是否有可能在git项目的所有分支中执行'grep搜索'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!