使用git,我如何在所有分支中搜索字符串?

时间:2011-08-22 17:41:53

标签: git search github branch

使用git,如何在所有本地分支的所有文件中搜索给定的字符串?

Github具体:是否可以在所有github分支上执行上述搜索? (在我的远程github repo上有几个远程分支,理想情况下我不需要为此搜索降低...)

5 个答案:

答案 0 :(得分:129)

您可以在Git仓库中执行此操作:

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

Github高级搜索具有代码搜索功能:

代码搜索将查看GitHub上公开托管的所有代码。您也可以按:

过滤
  • 语言:language:
  • 存储库名称(包括用户名):repo:
  • 文件路径:path:

答案 1 :(得分:96)

如果您使用@manojlds git grep命令并收到错误:

-bash: /usr/bin/git: Argument list too long" 

然后你应该使用xargs:

git rev-list --all | xargs git grep "string/regexp"

另见How to grep (search) committed code in the git history?

答案 2 :(得分:38)

在许多情况下,git rev-list --all可以返回大量的提交,需要永久扫描。如果您不是在存储库历史记录中的每个分支上搜索每个提交,而只想搜索所有分支提示,则可以将其替换为git show-ref --heads。所以总的来说:

git grep "string" `git show-ref --heads`

或:

git show-ref --heads | xargs git grep "string"

提示:您可以在文件中编写输出,以便在编辑器中查看。

nano ~/history.txt
git show-ref --heads | xargs git grep "search string here" >> ~/history.txt

答案 3 :(得分:11)

此处列出的解决方案几乎没有问题(甚至被接受)。

  1. 您不需要列出所有哈希值,因为您需要更多重复项,也需要更多时间。
  2. 它建立在此基础上,您可以在多个分支"test -f /"master上搜索字符串dev

    git grep "test -f /" master dev
    

    相同
    printf "master\ndev" | xargs git grep "test -f /"
    

    所以这里。

    这会查找所有本地分支的提示的哈希值,并仅在这些提交中进行搜索。

    git branch -v --no-abbrev | awk -F' *' '{print $3}' | xargs git grep "string/regexp"
    

    如果您还需要在远程分支中搜索,请添加-a

    git branch -a -v --no-abbrev | awk -F' *' '{print $3}' | xargs git grep "string/regexp"
    

    更新:

    # search in local branches
    git branch | cut -c3- | xargs git grep "string"
    # search in remote branches
    git branch -r | cut -c3- | xargs git grep "string"
    # search in all (local and remote) branches
    git branch -a | cut -c3- | cut -d' ' -f 1 | xargs git grep "string"
    
    # search in branches, and tags
    git show-ref | grep -v "refs/stash" | cut -d' ' -f2 | xargs git grep "string"
    

答案 4 :(得分:2)

你可以试试这个

head(data)

 time Global.horizontal.irradiance..W.m2.
1 2007-01-01 00:00:00                                   0
2 2007-01-01 01:00:00                                   0
3 2007-01-01 02:00:00                                   0
4 2007-01-01 03:00:00                                   0
5 2007-01-01 04:00:00                                   0
6 2007-01-01 05:00:00                                 159