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

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

问题描述

是否可以在git控件源项目的所有分支中运行git grep?还是有另一个命令来运行?

解决方案

问题如何在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






注意: chernjie 建议 是过度杀伤。


更精确的命令可以是:



  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 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>

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

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