Quickly Switch Git Branches

If you find yourself switching branches frequently and getting frustrated with typing out long branch names, or hitting tab way too many times to get to the branch you want to switch to, I might just have a solution for you!

This simple bash script will pull a list of your local branches and uses fzf to fuzzy-find the branch you want to checkout. The results are sorted by last commit date, so your most recent branches are right at the top.

branches=$(git for-each-ref --sort=-committerdate --format="%(refname:short)" refs/heads/)
current_branch=$(git symbolic-ref --short HEAD)
branch=$(echo "$branches" | grep -v "^$current_branch$" | fzf +m)

if [[ -n "$branch" ]]; then
	git co "$branch"
fi