Sunny Golovine

Fuzzy Git Branch Checkout

May 17, 2021

Hey everyone, like many developers I use JIRA for issue tracking at work. One thing to know about JIRA is the git integration it offers but with one caveat, the branches all have to follow a common naming convention. After a few weeks of creating branches, running git branch in my local repository yields a list that looks something like this:

....and more

If you have 3-5 branches on your local machine it's not that big of a deal, but get 20+ and it starts taking some effort to find the branch you're looking for, especially if you work in an environment where features are very close together so branches end up looking like


The other day I got tired of looking through this massive list of branches and asked myself: "can we make checking out these branches better?". So like any programmer, I made a script on my lunch break to fuzzy search these branches

The Script


function path-search() {
  if [ -z "$1" ]; then
    echo "Branch not supplied"
    return 1

  branch=$(git branch | grep $1 | xargs)

  if [ -z "$branch" ]; then
    echo "No matching braches found"
    return 1

  git checkout $branch
  echo "Successfully checked out branch $branch"
  return 0

This script is far from perfect but it gets the job done. Feel free to add this to your dot files and edit as you see fit.