Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Git remote all branches for file contents

Tags:

Is it possible to search all of my Git remote branches for specific file contents (not just a file, but contents within them)?

My remotes are on GitHub, in case that helps...

like image 213
professormeowingtons Avatar asked Aug 16 '13 17:08

professormeowingtons


People also ask

What is the git command to see all the remote branches?

To view your remote branches, simply pass the -r flag to the git branch command. You can inspect remote branches with the usual git checkout and git log commands.

Does git grep search all branches?

Git includes a grep command to search through commits to a repo as well as the local files in the repo directory: git grep. Sometimes it is useful to search for a string throughout an entire repo, e.g. to find where an error message is produced.


2 Answers

You can try this:

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

That will search all remote branches for search-string. Since the symbolic reference HEAD is mirrored, you may end up searching the same commit twice. Hopefully that's not an issue. If so, you can filter it out with:

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

If you need to dig through your entire history, you can also try:

git grep 'search-string' $(git rev-list --all) 
like image 157
John Szakmeister Avatar answered Oct 14 '22 20:10

John Szakmeister


Assuming you are tracking all remote branches, this will search it in all commits:

git log --all -p | grep 'search-string' 

To track all remote branches:

for remote in `git branch -r`; do git branch --track $remote; done 
like image 24
pawan jain Avatar answered Oct 14 '22 21:10

pawan jain