8

Is it possible to Git grep a string in one file in all branches?

I know I made a change to a particular file; I just can't find which branch I did it on.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Arrison
  • 1,464
  • 2
  • 13
  • 22
  • 3
    Possible duplicate of [Finding a Git commit that introduced a string in any branch](http://stackoverflow.com/questions/5816134/finding-a-git-commit-that-introduced-a-string-in-any-branch) –  May 13 '16 at 15:29

2 Answers2

7

Thanks to ElpieKay for providing this nice little gem:

git grep a_string $(git rev-parse --all -- target_file)
Michael Arrison
  • 1,464
  • 2
  • 13
  • 22
1

Use:

git stash
git branch | while read line
do
echo branch:$line
git checkout $line -- target_file
grep a_string target_file
done
git stash pop

Have a try.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ElpieKay
  • 27,194
  • 6
  • 32
  • 53