是否可以在 Git 项目的所有分支中执行“grep 搜索"? [英] Is it possible to perform a 'grep search' in all the branches of a Git project?

查看:29
本文介绍了是否可以在 Git 项目的所有分支中执行“grep 搜索"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Git 控制源项目的所有分支中运行 git grep?还是有其他命令可以运行?

解决方案

问题"如何在git 历史?"推荐:

 git grep $(git rev-list --all)

搜索所有提交,其中应该包括所有分支.

另一种形式是:

git rev-list --all |(在阅读修订时;做git grep -F 'yourWord' $revision完毕)

您可以在 这篇文章:

<块引用>

我在一个大到 git 抱怨参数大小的项目上尝试了上述方法,所以如果你遇到这个问题,请执行以下操作:

git rev-list --all |(虽然读 rev; 做 git grep -e  $rev; done)

(请参阅本答案最后一部分的替代方案,如下)

如果需要,请不要忘记这些设置:

# 允许扩展正则表达式git config --global grep.extendRegexp true# 总是包含行号git config --global grep.lineNumber true

这个别名也有帮助:

git config --global alias.g "grep --break --heading --line-number";


2016 年 8 月更新:R.M. 在评论中推荐

<块引用>

我得到一个fatal: bad flag '->'在文件名之后使用"尝试 git branch 版本时.该错误与 HEAD 别名符号相关联.

<块引用>

我通过在管道中在 trxargs 命令之间添加一个 sed '/->/d' 解决了这个问题.

 git branch -a |tr -d * |sed '/->/d' |xargs git grep 

即:

alias grep_all="git branch -a |tr -d * |sed '/->/d' |xargs git grep"grep_all 

这是对 chernjie 的解决方案的改进 建议,因为 git rev-list --all 是一种矫枉过正.

<块引用>

更精细的命令可以是:

#不要用这个,见上git 分支 -a |tr -d * |xargs git grep 

<块引用>

只允许搜索分支(包括远程分支)

<块引用>

你甚至可以为它创建一个 bash/zsh 别名:

#不要用这个,见上alias grep_all="git branch -a |tr -d * |xargs git grep"grep_all 

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"


Update August 2016: R.M. recommends in the comments

I got a "fatal: bad flag '->' used after filename" when trying the git branch version. The error was associated with a HEAD aliasing notation.

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>

That is:

alias grep_all="git branch -a | tr -d * | sed '/->/d' | xargs git grep"
grep_all <regexp>

This is an improvement over the solution chernjie had suggested, since git rev-list --all is an overkill.

A more refined command can be:

# Don't use this, see above
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:

# Don't use this, see above  
alias grep_all="git branch -a | tr -d * | xargs git grep"
grep_all <regexp>

这篇关于是否可以在 Git 项目的所有分支中执行“grep 搜索"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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