til/fuzzy switch git branch

Our branch naming convention at work follows this pattern: ABC-1234-some-name-for-the-branch, where ABC-1234 is the ID of the ticket in JIRA. Even with tab completion, I find it cumbersome to type git switch ABC-1234<tab>.

So, I've created a git alias that allows me to only type the ID's number (or even just a part of the number):

git config --global alias.fs "\!f() { git branch | grep $1 | xargs git switch; }; f"

Now, if I run git fs 1234, it will do:

  • !f(){ .... };f – This allows us to run arbitrary shell commands with a git alias
  • git branch – List all local branches.
  • grep $1 – Filter the list based on our input, $1, which is 1234.
  • xargs git switch – Switch to the filtered branch name.

The command will throw an error if the filtering returns more than one branch. If you don't have a lot of branches, you could even use git fs 34 to only match the end of the ID.

If you want to save even more keystrokes, you can alias git to g (alias g='git'). This allows us to use g fs 1234.