使用Git,在所有本地分支的所有文件中搜索给定字符串,应该如何操作?
GitHub特定问题:是否可以在GitHub的所有分支上执行上述搜索? (我的远程GitHub存储库上有几个远程分支,理想情况下我不希望为此搜索下载它们...)
GitHub特定问题:是否可以在GitHub的所有分支上执行上述搜索? (我的远程GitHub存储库上有几个远程分支,理想情况下我不希望为此搜索下载它们...)
你可以在Git仓库上执行这个操作:
git grep "string/regexp" $(git rev-list --all)
GitHub高级搜索具有代码搜索功能:
代码搜索将浏览GitHub上公开托管的所有代码。您还可以按以下方式进行过滤:
language:
repo:
path:
path:
,那将非常好,因为一眼看去的文档并不清楚在哪里应用此过滤器,我假设它在您的查询示例中的引号之前? - blamb如果你使用 @manojlds 的 Git grep 命令并出现错误:
-bash: /usr/bin/git: Argument list too long"
你应该使用xargs:
git rev-list --all | xargs git grep "string/regexp"
还可以看一下如何在Git历史中搜索已提交的代码
git rev-list --all | xargs -J % git grep "string/rexexp" % -- filename
。这个命令会在所有提交的代码块中运行 git grep
命令,并在代码块中 %
出现的地方插入指定的文件进行搜索。 - rgov-J
参数的作用是什么?因为 xargs -J
在某些版本的 xargs
中似乎不存在:https://man7.org/linux/man-pages/man1/xargs.1.html - Jann Poppingaxargs -I%echo%
将为每个输入行运行一次 echo <input line>
。 xargs -J%echo%
将运行 echo <input line 1> <input line 2> <input line 3> ...
,对于尽可能多的输入行放在一个命令中,从而减少调用次数。使用 -I
也可以,但会调用更多次 git grep
。 - rgov在很多情况下,git rev-list --all
可能会返回大量提交记录,需要花费很长时间进行扫描。如果你只想搜索所有分支的最新提交,而不是搜索仓库历史中每个分支的每个提交记录,可以使用git show-ref -s --heads
来替换它。所以总体上:
git grep "string" `git show-ref -s --heads`
git show-ref -s --heads | xargs git grep "string"
提示:您可以将输出写入文件以便在编辑器中查看:
nano ~/history.txt
git show-ref -s --heads | xargs git grep "search string here" >> ~/history.txt
git show-ref --heads
列出了哈希值和引用名称,因此它会进行两次搜索(第二行)。所以 git show-ref --heads | cut -d' ' -f2
更好,因为它只列出了引用名称。 - hIpPygit show-ref --heads -s
命令只输出SHA1哈希值。如果多个分支指向同一个提交,则会有重复。可以使用 sort -u
去掉重复项,例如 git show-ref --heads -s | sort -u | xargs git grep ...
。 - Stevefunction gsearch { git grep $1 $(git show-ref --heads) | grep "refs/heads" | grep $1 } # 最后一个grep用于保持颜色高亮显示
- AFP_555git grep -i "string" `git show-ref --heads | cut -d' ' -f2` -- '*.json'
- user3100212这里列出的解决方案存在一些问题(即使是被接受的)。
你不需要列出所有的哈希值,因为会重复。此外,这需要更多时间。
它基于这个思路:你可以在多个分支 master
和 dev
上搜索字符串 "test -f /"
。
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"
git branch -a | cut -c3- | cut -d' ' -f 1 | xargs git grep "string"
否则,在文件列表中出现带有 ->
符号的内容时会失败,该符号表示本地与远程分支之间的关系。 - Ilya Sheershoffcut -d' ' -f 1
。 - Anurag Pandegit branch
的 --format
参数: git branch -a --format“%(refname)”
只返回引用名称(每行一个)。 - creinig你可以尝试这个:
git log -Sxxxx # Search all commits
git log -Sxxxx --branches[=<pattern>] # Search branches
git log --all -s"搜索字符串"
- Tim Kuipersfatal: git upload-pack: not our ref <hash>
的错误。谢谢! - undefined跟随@peter-mortensen和manojlds的解决方案,我使用git for-each-ref
作为子命令来列出仅包含名称的分支。
git grep "string/regexp" $(git for-each-ref --format='%(refname:short)' refs/heads)
这样做可以更好地展现,仅显示具名分支并为每个分支生成一个结果。
for branch in $(git branch | awk '{print $1}'); do
echo "Branch: $branch"
git grep "SEARCH_WORD" $(git rev-parse $branch)
done
git log -S <search string> --source --all
https://dev59.com/sW025IYBdhLWcg3wwYz4#5816177
撤销一个提交,提交 ID 可能不在 HEAD
git revert commit_id
要忽略大小写,请使用-i:
git log -i --all --grep='word1 Word2'
--grep
的答案。实际上,这可能是因为它搜索的是 git 日志而不是所有分支中的文件内容,对吗?因此,它并不完全符合问题的要求。 - Andras Deak -- Слава Україні