搜索GIT远程所有分支的文件内容?

pro*_*ons 17 git github

是否可以在我的所有GIT远程分支中搜索特定的文件内容(不仅仅是文件,还有其中的内容)?

我的遥控器在github,如果有帮助......

Joh*_*ter 22

你可以试试这个:

git grep 'search-string' $(git ls-remote . 'refs/remotes/*' | cut -f 2)
Run Code Online (Sandbox Code Playgroud)

这将搜索所有远程分支search-string.由于符号引用HEAD是镜像的,因此您最终可能会两次搜索相同的提交.希望这不是问题.如果是这样,你可以用以下方法过滤掉它:

git grep 'search-string' \
    $(git ls-remote . 'refs/remotes/*' | grep -v HEAD | cut -f 2)
Run Code Online (Sandbox Code Playgroud)

如果您需要深入了解整个历史记录,您还可以尝试:

git grep 'search-string' $(git rev-list --all)
Run Code Online (Sandbox Code Playgroud)


小智 5

假设您正在跟踪所有远程分支,这将在所有提交中进行搜索.

git log --all -p | grep 'search-string'
Run Code Online (Sandbox Code Playgroud)

要跟踪所有远程分支:

for remote in `git branch -r`; do git branch --track $remote; done
Run Code Online (Sandbox Code Playgroud)