5

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,030
  • 21
  • 100
  • 124
Michael Arrison
  • 1,124
  • 2
  • 10
  • 20
  • 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

4

Thanks to ElpieKay for providing this nice little gem:

git grep a_string $(git rev-parse --all -- target_file)
Michael Arrison
  • 1,124
  • 2
  • 10
  • 20
0

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,030
  • 21
  • 100
  • 124
ElpieKay
  • 22,398
  • 5
  • 27
  • 44