Find A String in a Massive Git Repo
Doug Black

Doug Black @dougblackjr

About: I make robots obey the commands of the human masters! Founder of Engauge Analytics

Location:
Philadelphia, PA
Joined:
Mar 22, 2017

Find A String in a Massive Git Repo

Publish Date: May 8 '19
90 7

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?

Comments 7 total

  • Mpho Mphego
    Mpho MphegoMay 9, 2019

    Nice, just what I needed.
    Thanks.

  • Christophe El-Khoury
    Christophe El-KhouryMay 10, 2019

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

    • Doug Black
      Doug BlackMay 10, 2019

      I want sure what branch the change was in.

  • Adam Parkin
    Adam ParkinMay 10, 2019

    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.

  • Haggä
    HaggäSep 9, 2020

    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.

  • Marcos
    MarcosFeb 1, 2022

    great

Add comment