Skip to main content

Git Branch Search 🔎

You find a bug, and you find the commit that introduced the bug. Now you need to find which release branches are affected.

Looking for the commit sha probably won’t work; cherry-picking or rebasing changes the sha. What will work is searching for the commit message:

git log \
  --all \                       # log commits on all branches
  --grep="My commit message!" \ # search for a commit message
  --format="%H"                 # print just commit sha

This will print out a list of commit shas:

dd7df1e293294e80811e9e1bd78414b3cacc0656
622fa9e63c458bcf5ffb3acbef3f5748cdb6cf8c

To print the branches containing these commits, use xargs and git branch:

git log --all --grep="My commit message!" --format="%H" |\
  xargs \                     # pipe each line of stdin to command
    git branch \              # for each line, run git branch
      --all \                 # all branches, including remote
      --list "*/releases/*" \ # filter to release branches only
      --contains              # containing the commit sha

Now you know which branches need the hotfix you just cooked up!