Search in all git branches

In this brief article, we demonstrate how to search for text within all local Git branches. There may be instances when you have numerous Git branches, making it challenging to recall in which branch a particular function resides. The bash code below loops through all of your local Git branches and uses grep to search for a specified pattern:

for b in $(git for-each-ref --format="%(refname:short)" | awk '{print $0}'); 
do 
    echo $b 
    git checkout $b 
    grep -R BitmapFactory .  
done

This script searches for BitmapFactory text in all the branches. Replace it with the required text.

The code can be succinctly expressed in a single line:

for b in $(git for-each-ref --format="%(refname:short)" | awk '{print $0}'); do echo $b; git checkout $b; grep -R BitmapFactory .;  done
bash  git 

See also