Git checkout with fuzzy finder
I use both CLI and GUI apps for managing git.
One thing that I love when using git GUI apps is that I can fuzzy find branch names and switch to them easily. This is especially true when I use Fork or Tower. Both of them have keyboard shortcuts to perform various actions on branches or git repositories.
We can actually replicate this functionality in the command line. First, we need a fuzzy finder, fzf. You can install it with brew:
brew install fzf
With fzf
, you can pass any list, filter the list as you type, and select an item.
Here's what we're going to do:
- List out all branches in the git repository
- Pass them to fzf
- When we select a branch, we'll clean the string and extract the branch name
- Perform git checkout on that branch
The full command:
git checkout $(git branch --list | fzf | tr -d ' *')
You can create an alias for it. Mine is set to gfc
:
alias gfc='git checkout $(git branch --list | fzf | tr -d " *")'
This simple trick will save you time and make branch switching in the terminal as convenient as using a GUI. Give it a try and let me know what you think!