Is it possible to perform a 'grep search' in all the branches of a Git project? Is it possible to perform a 'grep search' in all the branches of a Git project? git git

Is it possible to perform a 'grep search' in all the branches of a Git project?


The question "How to grep (search) committed code in the git history?" recommends:

 git grep <regexp> $(git rev-list --all)

That searches through all the commits, which should include all the branches.

Another form would be:

git rev-list --all | (    while read revision; do        git grep -F 'yourWord' $revision    done)

You can find even more example in this article:

I tried the above on one project large enough that git complained about the argument size, so if you run into this problem, do something like:

git rev-list --all | (while read rev; do git grep -e <regexp> $rev; done)

(see an alternative in the last section of this answer, below)

Don't forget those settings, if you want them:

# Allow Extended Regular Expressionsgit config --global grep.extendRegexp true# Always Include Line Numbersgit config --global grep.lineNumber true

This alias can help too:

git config --global alias.g "grep --break --heading --line-number"

Note: chernjie suggested that git rev-list --all is an overkill.

A more refined command can be:

git branch -a | tr -d \* | xargs git grep <regexp>

Which will allow you to search only branches (including remote branches)

You can even create a bash/zsh alias for it:

alias grep_all="git branch -a | tr -d \* | xargs git grep"grep_all <regexp>

Update August 2016: R.M. recommends in the comments

I got a "fatal: bad flag '->' used after filename" when trying the git branch version. The error was associated with a HEAD aliasing notation.

I solved it by adding a sed '/->/d' in the pipe, between the tr and the xargs commands.

 git branch -a | tr -d \* | sed '/->/d' | xargs git grep <regexp>

That is:

alias grep_all="git branch -a | tr -d \* | sed '/->/d' | xargs git grep"grep_all <regexp>


git log can be a more effective way of searching for text across all branches, especially if there are many matches, and you want to see more recent (relevant) changes first.

git log -p --all -S 'search string'git log -p --all -G 'match regular expression'

These log commands list commits that add or remove the given search string/regex, (generally) more recent first. The -p option causes the relevant diff to be shown where the pattern was added or removed, so you can see it in context.

Having found a relevant commit that adds the text you were looking for (eg. 8beeff00d), find the branches that contain the commit:

git branch -a --contains 8beeff00d


I found this most useful:

git grep -i foo `git for-each-ref --format='%(refname)' refs/`

You'd need to adjust the last arguments depending on whether you want to only look at remote vs. local branches, i.e.:

  • git grep -i foo $(git for-each-ref --format='%(refname)' refs/remotes)
  • git grep -i foo $(git for-each-ref --format='%(refname)' refs/heads)

The alias I created looks like this:

grep-refs = !sh -c 'git grep "$0" "$@" "$(git for-each-ref --format=\"%(refname)\"" refs/)'