是否可以在 Git 项目的所有分支中执行“grep 搜索”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15292391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:41:48  来源:igfitidea点击:

Is it possible to perform a 'grep search' in all the branches of a Git project?

gitgrep

提问by epsilones

Is it possible to run git grepinside all the branches of a Git control sourced project? Or is there another command to run?

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

回答by VonC

The question "How to grep (search) committed code in the git history?" recommends:

问题“如何在 git 历史记录中 grep(搜索)提交的代码?”建议:

 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 抱怨参数大小的项目上尝试了上述方法,因此如果遇到此问题,请执行以下操作:

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: chernjiesuggestedthat git rev-list --allis an overkill.

注:chernjie建议git rev-list --all是矫枉过正。

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:

这将允许您仅搜索分支(包括远程分支)

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

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

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

2016 年 8 月更新:RM在评论中推荐

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

I solved it by adding a sed '/->/d'in the pipe, between the trand the xargscommands.

fatal: bad flag '->' used after filename尝试git branch版本时我得到了一个“ ” 。该错误与HEAD别名符号相关联。

我通过sed '/->/d'在管道中,在trxargs命令之间添加一个来解决它。

 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>

回答by Edward Anderson

git logcan be a more effective way of searching for text across all branches, especially if there are many matches, and you want to see more recent (relevant) changes first.

git log可以是在所有分支中搜索文本的更有效方式,尤其是在有很多匹配项并且您希望首先查看更多最近(相关)更改的情况下。

git log -p --all -S 'search string'
git log -p --all -G 'match regular expression'

These log commands list commits that add or remove the given search string/regex, (generally) more recent first. The -poption causes the relevant diff to be shown where the pattern was added or removed, so you can see it in context.

这些日志命令列出添加或删除给定搜索字符串/正则表达式的提交,(通常)首先是最近的。该-p选项会导致相关差异显示在添加或删除模式的位置,因此您可以在上下文中查看它。

Having found a relevant commit that adds the text you were looking for (eg. 8beeff00d), find the branches that contain the commit:

找到添加了您要查找的文本的相关提交(例如 8beeff00d)后,找到包含该提交的分支:

git branch -a --contains 8beeff00d

回答by errordeveloper

I found this most useful:

我发现这最有用:

git grep -i foo `git for-each-ref --format='%(refname)' refs/`

You'd need to adjust the last arguments depending on whether you want to only look at remote vs. local branches, i.e.:

您需要根据您是否只想查看远程与本地分支来调整最后一个参数,即:

  • git grep -i foo $(git for-each-ref --format='%(refname)' refs/remotes)
  • git grep -i foo $(git for-each-ref --format='%(refname)' refs/heads)
  • git grep -i foo $(git for-each-ref --format='%(refname)' refs/remotes)
  • git grep -i foo $(git for-each-ref --format='%(refname)' refs/heads)

The alias I created looks like this:

我创建的别名如下所示:

grep-refs = !sh -c 'git grep "
git grep-branch "find my text"
git grep-branch --some-grep-options "find my text"
" "$@" "$(git for-each-ref --format=\"%(refname)\"" refs/)'

回答by Devaldo

It's possible to do it in two common ways: Bash or Git aliases

可以通过两种常用方式来实现:Bash 或 Git 别名

Here are three commands:

下面是三个命令:

  1. git grep-branch- Search in all branches local & remote
  2. git grep-branch-local- Search in local branches only
  3. git grep-branch-remote- Remote branches only
  1. git grep-branch- 在本地和远程的所有分支机构中搜索
  2. git grep-branch-local- 只在本地分行搜寻
  3. git grep-branch-remote- 仅远程分支

Usage is the same as git grep

用法与 git grep


[alias]
    grep-branch        = "!f(){ git branch -a | sed -e 's/[ \*]*//' | grep -v -e '\->' | xargs git grep $@; };f "
    grep-branch-remote = "!f(){ git branch -a | sed -e 's/[ \*]*//' | grep -v -e '\->' | grep '^remotes' | xargs git grep $@; };f"
    grep-branch-local  = "!f(){ git branch -a | sed -e 's/[ \*]*//' | grep -v -e '\->' -e '^remotes' | xargs git grep $@;  };f "

GREP using: Git aliases

GREP 使用:Git 别名

File ~/.gitconfig

文件 ~/.gitconfig

Commands should be added manually to ~/.gitconfigfile, because git config --global aliasevaluate complex code you add and mess it up.

命令应该手动添加到~/.gitconfig文件中,因为git config --global alias评估您添加的复杂代码并将其弄乱。

[user@pc project]$ git grep-branch-local -n getTastyCookies

dev:53:modules/factory/getters.php:function getTastyCookies($user);
master:50:modules/factory/getters.php:function getTastyCookies($user)

Note: When you add aliases and they fail to run - check backslashes \they may require additional escape \\in compare to bashcommands.

注意:当您添加别名并且它们无法运行时 - 检查反斜杠,与bash命令相比,\它们可能需要额外的转义。\\

  • git branch -a- Display all branches;
  • sed -e 's/[ \\*]*//'- Trim spaces(from branch -a) and * (active branch name have it);
  • grep -v -e '\\->'- Ignore complex names likeremotes/origin/HEAD -> origin/master;
  • grep '^remotes'- Get all remote branches;
  • grep -v -e '^remotes'- Get branches exceptremote branches;
  • git branch -a- 显示所有分支;
  • sed -e 's/[ \\*]*//'- 修剪空格(来自branch -a)和*(活动分支名称有);
  • grep -v -e '\\->'- 忽略复杂的名称,如remotes/origin/HEAD -> origin/master;
  • grep '^remotes'- 获取所有远程分支;
  • grep -v -e '^remotes'- 获取远程分支外的分支;

Example git grep-branch-local -n getTastyCookies

例子 git grep-branch-local -n getTastyCookies

-nPrefix the line number to matching lines.

-n将行号作为匹配行的前缀。

git branch -a | sed -e 's/[ \*]*//' | grep -v -e '\->' -e '^remotes' | xargs git grep "TEXT"

The current structure is:

目前的结构是:

:- Separator

:- 分隔器

  1. Branch: dev
  2. Line number: 53
  3. File path: modules/factory/getters.php
  4. Matching line: function getTastyCookies($user)
  1. 分支: dev
  2. 电话号码: 53
  3. 文件路径: modules/factory/getters.php
  4. 匹配线: function getTastyCookies($user)

GREP using: BASH

GREP 使用:BASH

As you should know: Bash commands should be stored in .shscripts or run in a shell.

您应该知道:Bash 命令应该存储在.sh脚本中或在 shell 中运行。

Local branches only

仅限本地分行

git branch -a | sed -e 's/[ \*]*//' | grep -v -e '\->' | grep '^remotes' | xargs git grep "TEXT"

Remote branches only

仅远程分支

git branch -a | sed -e 's/[ \*]*//' | grep -v -e '\->' | xargs git grep "TEXT"

Local & remote branches

本地和远程分支机构

git for-each-ref --format='%(*refname)' | xargs git grep SEARCHTERM

回答by William Entriken

Here's how I do it:

这是我的方法:

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

回答by CharlesB

If you give any commit a SHA-1 hash value to git grepyou have it search in them, instead of the working copy.

如果你给任何提交一个 SHA-1 哈希值,git grep你让它在其中搜索,而不是工作副本。

To search all branches, you can get allthe trees with git rev-list --all. Put it all with

要搜索所有分支,你可以得到所有与树木git rev-list --all。把这一切与

##代码##

... and have patience

...并有耐心