21

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...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
professormeowingtons
  • 3,504
  • 7
  • 36
  • 50

2 Answers2

29

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)
John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
  • Second command works for me, but the first doesn't; I get `fatal: bad flag '->' used after filename` – pattivacek Aug 16 '13 at 17:46
  • 1
    Sorry about that. I forgot that `git branch -r` will show the contents of symbolic refs in it. I've updated my answer with something else that works, but not quite as pretty. – John Szakmeister Aug 16 '13 at 20:02
6

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pawan jain
  • 139
  • 2
  • 6