Skip to content

Instantly share code, notes, and snippets.

@austinhyde
Created January 25, 2021 22:23
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 austinhyde/1350b57c8118646462b843b1dbbec78b to your computer and use it in GitHub Desktop.
Save austinhyde/1350b57c8118646462b843b1dbbec78b to your computer and use it in GitHub Desktop.
Fuzzy Git Checkout - For when you have too many branches
# requires fzf
# co -: check out last branch
# co -anything at all: just does a plain git checkout
# co foo: check out the branch with the closest name to "foo"
function co() {
if [[ $1 == -* ]]; then
git checkout "$@";
else
git checkout "$(git for-each-ref --format='%(refname:short)' refs/heads/** | fzf -f "$1" | head -1)";
fi
}
# coi: show a an interactive branch selector with preview of last commit+diff
# coi foo: show the interactive selector with "foo" pre-typed
function coi() {
sel="$(git for-each-ref --format='%(refname:short)' refs/heads/** | fzf --preview 'git show --color {}' -q "$1")" || return $?;
git checkout "$sel"
}
@austinhyde
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment