Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@timuruski
Last active March 23, 2017 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timuruski/235fbed695f328415bfd07ca72d4351b to your computer and use it in GitHub Desktop.
Save timuruski/235fbed695f328415bfd07ca72d4351b to your computer and use it in GitHub Desktop.
Git Fuzzy/Interactive Checkout
[alias]
ci = "!source ~/.githelpers && fuzzy_checkout"
fuzzy_checkout() {
if [[ $# -gt 0 ]]; then
git branch | fzf --exact --select-1 --query $1 | xargs git checkout
else
git branch | fzf | xargs git checkout
fi
}

Git Fuzzy/Interactive Checkout

This is just a quick hack to add interactive a sort of fuzzy-matching interactive checkout for Git. The problem this solves is branches with ticket numbers in the name, which aren't as easy to memorize or auto-complete.

Prerequisites and Installation

This requires fzf, which can be installed from Github or homebrew.

$ brew install fzf

To install, just copy the corresponding lines from .gitconfig into your Git config, and add the .githelpers file to your home directory. If you already have an equivalent file, append the function and tweak the alias in .gitconfig.

Usage

$ git branch
123-foo
234-bar
345-qux

$ git ci
> 123-foo
  234-bar
  345-qux
# Type foo and hit enter to checkout 123-foo

$ git ci foo
# Checks out 123-foo because it's the only match.

$ git ci 23
> 123-foo
  234-bar
# Pre-filters 123-foo and 234-bar, but doesn't checkout due to ambiguity

$ git ci blah
# No match, no operation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment