I have a very handy shell alias (a shortcut in the terminal) that allows me to easily switch between my most recent Git branches using fzf

What is FZF Link to heading

It’s a tiny little “fuzzy finder”. Fuzzy search means to find strings that matches patterns approximately. For example if you have a string called database-server-12 and you want to fuzzy find it, you maybe able to use db12 to easily find it

The Problem Link to heading

When we work on a project, we often have to switch between several different branches, be it between a main and a feature branch, or between several feature branches. Remembering all of them can be difficult

The Solution Link to heading

Luckily, it is pretty straight forward to come up with an alias that makes our day a lot more easier (at least for switching between git branches)

Install FZF Link to heading

Install it following the instructions corresponding to your operating system

If you use Ubuntu or other debian based distros

sudo apt update
sudo apt install fzf

Fedora and other RedHat based distros

sudo dnf install fzf

MacOS

brew install fzf

Windows (WSL)

Depending on the OS you use, you can use apt or dnf as mentioned above

The Alias Link to heading

If you use bash, add the following to your ~/.bashrc file or If you use zsh, add it to the ~/.zshrc

alias gb="git checkout \$(git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/ | fzf)"

That should be it. reload your terminal or source ~/.bashrc or source ~/.zshrc to reload your rc file and you are ready to use the alias.

Obviously you can use any shortcut instead of gb. Now just type gb in a git repo and you can use your arrow key and then press enter to choose the branch easily