0

I have a private git repo where I have a keyword like abcde I previously wrote down in 1 of the files that may or may not still be in the repo today. Is there a simple way to search all the files of all previous commits for my keyword abcde?

I'm thinking I need to write a script to search every current file but if it's not present just start git reset --hard previousCommit until it reaches the very 1st commit.

letter Q
  • 14,735
  • 33
  • 79
  • 118
  • 2
    Does this answer your question? [Using Git, how could I search for a string across all branches?](https://stackoverflow.com/questions/7151311/using-git-how-could-i-search-for-a-string-across-all-branches) – snakecharmerb Jul 24 '21 at 18:24
  • No I need it for all previous commits on 1 branch but thanks for the info. @snakecharmerb – letter Q Jul 24 '21 at 18:50

2 Answers2

1

You are probably looking for

git log -G mykeyword 

With a single git log command, you can search for the commit that introduces any given text.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • There is also the Pickaxe. See for instance http://www.philandstuff.com/2014/02/09/git-pickaxe.html – matt Jul 24 '21 at 18:21
  • I just tried this & it gave me 3 commits out of ~2500 where 2/3 of the commits did have `mykeyword`. It seems to not be working exactly like what I need because I know `mykeyword` exists in more than 3 out of the ~2500 commits but thanks for the info. – letter Q Jul 24 '21 at 18:44
  • Ah, no, you don't need to know where it exists, only the endpoints where the existence changes. Please read the doc on git log. – matt Jul 24 '21 at 19:37
  • @Wang-Zhao-LiuQ You might have gotten an extra commit because this command also includes commits that *remove* text. Without seeing the exact commit logs, I can't say for sure what you're seeing. But that's likely it. – siride Jul 25 '21 at 14:00
0

You would have to iterate two lines of code N times:
(if you have N commits for example)

for VARIABLE in 1 .. N
do
  git checkout HEAD^
  grep keyword *
done

So, in your case, you can copy and paste the following code into git bash:

for VARIABLE in 1 .. 2500
do
  git checkout HEAD^
  grep abcde *
done

The iteration stops when the next commit no more presents what you are looking for.
Once this is done, you can finally see what the SHA identifier of the oldest commit is that has the word you are looking for. And then possibly apply the "reset --hard"

I hope I have been helpful.

Giuseppe Amato
  • 103
  • 1
  • 1
  • 7