使用 Git,如何在所有分支中搜索字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7151311/
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
Using Git, how could I search for a string across all branches?
提问by Ivar
Using Git, how could I search within all files in all local branches for a given string?
使用 Git,如何在所有本地分支的所有文件中搜索给定字符串?
GitHub specific: is it possible to perform the above search across all GitHub branches? (There are several remote branches on my remote GitHub repository that ideally I wouldn't have to bring down for this search...)
GitHub 特定:是否可以在所有 GitHub 分支上执行上述搜索?(我的远程 GitHub 存储库上有几个远程分支,理想情况下我不必为此搜索而关闭......)
采纳答案by manojlds
You can do this on a Git repository:
您可以在 Git 存储库上执行此操作:
git grep "string/regexp" $(git rev-list --all)
GitHub advanced search has code search capability:
GitHub 高级搜索具有代码搜索功能:
The code search will look through all of the code publicly hosted on GitHub. You can also filter by:
代码搜索将查看 GitHub 上公开托管的所有代码。您还可以按以下条件过滤:
- the language:
language:
- the repository name (including the username):
repo:
- the file path:
path:
- 语言:
language:
- 存储库名称(包括用户名):
repo:
- 文件路径:
path:
回答by teastburn
If you use @manojlds Git grep command and get an error:
如果你使用@manojlds Git grep 命令并得到一个错误:
-bash: /usr/bin/git: Argument list too long"
then you should use xargs:
那么你应该使用xargs:
git rev-list --all | xargs git grep "string/regexp"
Also see How to grep (search) committed code in the Git history
回答by Zitrax
In many cases git rev-list --all
can return a huge number of commits, taking forever to scan. If you, instead of searching through every commit on every branch in your repository history, just want to search all branch tips, you can replace it with git show-ref --heads
. So in total:
在许多情况下,git rev-list --all
可以返回大量提交,需要永远扫描。如果您不想搜索存储库历史记录中每个分支上的每个提交,而只想搜索所有分支提示,则可以将其替换为git show-ref --heads
. 所以总的来说:
git grep "string" `git show-ref --heads`
or:
或者:
git show-ref --heads | xargs git grep "string"
Tip:You can write output in file to view in an editor:
提示:您可以将输出写入文件以在编辑器中查看:
nano ~/history.txt
git show-ref --heads | xargs git grep "search string here" >> ~/history.txt
回答by hIpPy
There are a few issues with the solutions listed here (even accepted).
此处列出的解决方案存在一些问题(甚至被接受)。
You do not need to list all the hashes as you'll get duplicates. Also, it takes more time.
你不需要列出所有的哈希值,因为你会得到重复的。此外,还需要更多时间。
It builds on this where you can search a string "test -f /"
on multiple branches master
and dev
as
它建立在此基础上,您可以"test -f /"
在多个分支上搜索字符串,master
并dev
作为
git grep "test -f /" master dev
which is same as
这与
printf "master\ndev" | xargs git grep "test -f /"
So here goes.
所以就到这里了。
This finds the hashes for the tip of all local branches and searches only in those commits:
这将查找所有本地分支的尖端的哈希值,并仅在这些提交中搜索:
git branch -v --no-abbrev | awk -F' *' '{print }' | xargs git grep "string/regexp"
If you need to search in remote branches too then add -a
:
如果您也需要在远程分支中搜索,请添加-a
:
git branch -a -v --no-abbrev | awk -F' *' '{print }' | xargs git grep "string/regexp"
Further:
更远:
# 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"
回答by Victor Choy
You can try this:
你可以试试这个:
git log -Sxxxx # Search all commits
git log -Sxxxx --branches[=<pattern>] # Search branches