DEV Community

Doug Black
Doug Black

Posted on

Find A String in a Massive Git Repo

Figure I would share this, as I just needed to do this.

We have a massive git repo for an Engauge Analytics project we are working on. Tons of these branches have active work being done, and some were just skipped during the pruning process.

I needed to find 10 characters.

Technically, I needed to find the method that contained those 10 characters, but I knew those 10 characters would be the key to finding it.

Let's say you're looking for a string title the magic string in one of your many git branches in your repo. You can use:

git grep "the magic string" `git show-ref --heads`
Enter fullscreen mode Exit fullscreen mode

If the string exists in your repo, anywhere, you'll get something like this:

***c5cd1d8:path/to/file.js:let a = "the magic string"
Enter fullscreen mode Exit fullscreen mode

Once you have that data, it's as easy as finding the branch!

git branch --contains ***c5cd1d8
Enter fullscreen mode Exit fullscreen mode

There may be an easier way to do this. What do you recommend?

Top comments (7)

Collapse
 
pzelnip profile image
Adam Parkin

You might want to check out the options on the git log command as well, as it can be used to search across all history rather than just all current branches. For example to search all commit messages for the string "foo bar baz":

git log --grep="foo bar baz"
Enter fullscreen mode Exit fullscreen mode

If you want to search file contents, you can use the "pickaxe". Not sure how you do multiple words, but for the single word "foo":

git log -Sfoo
Enter fullscreen mode Exit fullscreen mode

This will search all commits for the word "foo" being added or removed.

Collapse
 
dougblackjr profile image
Doug Black

Awesome! Thank you!

Collapse
 
gahamelan profile image
Haggä

Hi! Great post! I was in need of the same thing. One question, though. Does this search across all branches but also across all commits inside each branch (=the whole repo, at each of the stages)? Thanks.

Collapse
 
mmphego profile image
Mpho Mphego

Nice, just what I needed.
Thanks.

Collapse
 
christopheek profile image
Christophe El-Khoury

So why wouldn't you simply pull that branch locally and search for the string using your IDE / Editor?

Collapse
 
dougblackjr profile image
Doug Black

I want sure what branch the change was in.

Collapse
 
marcospatton profile image
Marcos

great