使用 Git,我如何在所有分支中搜索字符串?
使用 Git,我如何在所有本地分支的所有文件中搜索给定的字符串?
GitHub 具体:是否可以在所有 GitHub 分支上执行上述搜索? (我的远程 GitHub 存储库上有几个远程分支,理想情况下我不必为这次搜索而关闭它们......)
Using Git, how could I search within all files in all local branches for a given string?
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...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以在 Git 存储库上执行此操作:
GitHub 高级搜索具有代码搜索功能:
代码搜索将查找 GitHub 上公开托管的所有代码。您还可以按以下条件进行过滤:
语言:
repo:
路径:
You can do this on a Git repository:
GitHub advanced search has code search capability:
The code search will look through all of the code publicly hosted on GitHub. You can also filter by:
language:
repo:
path:
如果您使用 @manojlds Git grep 命令并收到错误:
那么您应该使用 xargs:
另请参阅如何 grep(搜索)提交Git 历史记录中的代码
If you use @manojlds Git grep command and get an error:
then you should use xargs:
Also see How to grep (search through) committed code in the Git history
在许多情况下, git rev-list --all 可能会返回大量提交,需要很长时间才能扫描。如果您不想搜索存储库历史记录中每个分支上的每个提交,而只想搜索所有分支提示,则可以将其替换为 git show-ref -s --heads 。因此总共:
或:
提示:您可以在文件中写入输出以在编辑器中查看:
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 withgit show-ref -s --heads
. So in total:or:
Tip: You can write output in file to view in an editor:
此处列出的解决方案存在一些问题(甚至已被接受)。
您不需要列出所有哈希值,因为您会得到重复项。而且,还需要更多的时间。
它建立在这个基础上,您可以在多个分支
master
和dev
上搜索字符串"test -f /"
,相同
与此处 去。
这会查找所有本地分支尖端的哈希值,并仅在这些提交中搜索:
如果您也需要在远程分支中搜索,则添加
-a
:进一步:
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 branchesmaster
anddev
aswhich is same as
So here goes.
This finds the hashes for the tip of all local branches and searches only in those commits:
If you need to search in remote branches too then add
-a
:Further:
你可以试试这个:
You can try this:
关注 @peter-mortensen & manojlds 的解决方案,我使用 git for-each-ref 作为子命令来仅列出具有名称的分支。
这实现了更好的可视化,仅显示命名分支并为每个分支仅生成一个结果。
Following @peter-mortensen & manojlds's solution, I use
git for-each-ref
as subcommand to list only branches with name.This accomplish a better visualization, showing only named braches and making only one result for each branch.
要显示分支名称以及搜索结果,您可以使用循环分别搜索每个分支,如下所示:
此循环使用 gitbranch 列出所有分支,并使用 awk 仅提取分支名称。然后,它使用 git rev-parse 获取每个分支的提交哈希,并使用 git grep 在该分支中搜索字符串“deleteTemplateDocument”。输出将显示分支名称以及每个分支的匹配结果。
https://stackoverflow.com/a/5816177/5368856
恢复提交,提交 ID 可能不在 HEAD
To display the branch name along with the search results, you can use a loop to search each branch separately, like this:
This loop uses git branch to list all branches, and awk to extract just the branch names. Then, it uses git rev-parse to get the commit hash of each branch, and git grep to search for the string "deleteTemplateDocument" in that branch. The output will show the branch name and the matching results for each branch.
https://stackoverflow.com/a/5816177/5368856
Revert a commit, commit id may not be at HEAD
对我来说它是:
git分支:在本地查找分支。
git grep -c :查找给定分支中的字符串。
<强>| xargs :一个管道和 xargs 将分支作为上一个命令的输入。
For me it is :
git branch : looks for branchs in local.
git grep -c : looks for the strings in a given branch.
| xargs : a pipe and xargs to give the branches as inputs to the previous command.
高度可定制的终端脚本:
此命令将帮助您在以“/master”结尾的分支名称中的各种编程语言的文件(可选)中搜索特定字符串(可以是正则表达式) “ Git 存储库中的(可选)。
git brach -r
:所有分支名称的列表grep "/master$"
:(可选)使用正则表达式过滤分支名称,仅搜索以 /master 结尾的分支git grep
:搜索的主程序--no-pager
:(可选)一次显示所有结果,不使用分页程序(滚动显示更多)。-E
:此选项启用搜索字符串的扩展正则表达式语法。-n
:此选项显示行号以及匹配的行。"(搜索字符串 1|搜索字符串 2...)"
:您可以在此处进行正则表达式-- "*.js" "*.ts" "*.kt" "*. swift" "*.json"
:(可选)搜索特定文件类型,根据需要添加或删除Highly customizable terminal script:
This command will help you search for specific strings (can be regex) in files of various programming languages (optional) within branches name ended with "/master" (optional) in a Git repository.
git brach -r
: List of all branches namegrep "/master$"
: (optional) filter branch name using regex, only search branches that end with /mastergit grep
: Main program to search--no-pager
: (optional) Display all result at once, not using pager program (scroll to display more).-E
: This option enables extended regular expression syntax for the search strings.-n
: This option displays line numbers along with the matching lines."(search string 1|search string 2...)"
: you can you regex here-- "*.js" "*.ts" "*.kt" "*.swift" "*.json"
: (optional) Search with specific file types, add or remove as you want要忽略大小写,请使用 -i:
To ignore case use -i:
这是一种多进程方法,通过为每个分支或提交生成一个 git grep 进程来显着提高搜索速度:
有关每个命令的完整说明以及更多信息,请参阅我的完整内容在这里回答:有关在 git 存储库中搜索(通过
grep
或类似方式)的所有内容。Here's a multi-process way that dramatically increases the speed of the search by spawning one
git grep
process per branch or commit:For a full explanation of each command, and a ton more info., see my full answer here: All about searching (via
grep
or similar) in your git repositories.