If you are building an app with a long history, you may have a lot of git branches laying around. Generally you can use your terminal’s auto completion to quickly resolve branch names with minimal typing. However, there may be cases where there are branch names that start with the same characters.

With this git alias, you can search your git branches by partial match; then switch to the first match.

[alias]
find-branch = !sh -c \"git branch -a | grep -v remotes | grep $1 | head -n 1 | xargs git checkout\"

Put this in your ~/.gitconfig or project .git/config file and then try it out:

  > git find-branch sta
  Switched to branch 'staging'
  Your branch is up-to-date with 'origin/staging'.

Switch out “sta” in the example above with whatever partial branch name you like.

So what’s going on inside the command? First off - the entire command is embedded in an !sh call. This isn’t required for simpler git aliases - for instance:

  s       = status --short
  b       = branch
  co      = checkout
  changes = diff --name-status -w

However, wrapping the branch switching alias in !sh allows us to properly form the parameter in the grep command. The exclamation point tells git the alias should be executed and the -c parameter tells sh to run an in-line command.

There are five parts to this command - let’s look at each step:

[alias]
find-branch = !sh -c \"git branch -a\"

This lists all branches, including remotes:

> git find-branch
master
staging
staging-v1
statging-v2
remotes/origin/staging
remotes/origin/staging-v1
remotes/origin/staging-v2

Now let’s filter out the remotes - this version operates on local branches only.

[alias]
find-branch = !sh -c \"git branch -a | grep -v remotes\"

The output should look something like this:

> git find-branch
master
staging
staging-v1
statging-v2

Next we filter the results to match a pattern:

[alias]
find-branch = !sh -c \"git branch -a | grep -v remotes | grep $1\"

We’re nearly to the results we want:

> git find-branch sta
staging
staging-v1
staging-v2

Grab the top result:

[alias]
find-branch = !sh -c \"git branch -a | grep -v remotes | grep $1 | head -n 1\"
> git find-branch sta
staging

Finally, apply the result to a git checkout command.

[alias]
find-branch = !sh -c \"git branch -a | grep -v remotes | grep $1 | head -n 1 | xargs git checkout\"
> git find-branch sta
Switched to branch 'staging'
Your branch is up-to-date with 'origin/staging'.

That’s it. I’ve found this alias pretty useful in large projects. In the future I may expand this to allow remote branch switching as well. I’d love to hear your thoughts - what variations are there to simplify or augment the alias?