git branch checkout made easy with fzf

What it is

FZF to create a zsh functiona to interactively select a git branch to check out.

FZF is a nice tool for all kinds of fuzzy finding on your computer. It really shines when combined with other tools to make them interactive.

What it does for you

The function we create here can do the following for us:

  • show all branches of the current repository as a list
  • optinally takes an fzf query string to prepopulate fzf
  • allow for fuzzy-finding the one we want with fzf interactively
  • help with the selection by showing the commit history of that branch in fzf's preview
  • select one and automatically check it out

The Code

function gcx() {
  my_branch=$(git branch -a --no-color | sort |uniq | tr -d " " | fzf --select-1 --ansi --preview 'git log --graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr" {} 2>/dev/null')
  if echo $my_branch | grep -q "remotes/origin"; then
  if echo "$my_branch" | grep -q -P --regexp='\*'; then

  git checkout $my_branch

How to use it

Put the code into a file, e.g. fzf_git.zsh and source it. If you like it, add the command to source it to one of your shell initialization scripts.

Then run


# or provide some (fuzzy) search text to start with

gcx feat

and it will look similarly to this:

git checkout with fzf

to find the branch you want to checkout, type some more search characters - it can be a fuzzy-find - and/or use the arrow keys to select a branch.

Then press <Enter> to check the selected branch out. Use <ESC> to exit without checking out.

You can also use this command to quickly get some overview about the commit history in different branches.