Last active
April 4, 2025 13:42
-
-
Save drewbrokke/35b2f5fcb424d908b25760a3237b5bb1 to your computer and use it in GitHub Desktop.
Fast git checkout using `fzf`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Requires 'fzf' | |
# check_dependency fzf git || exit 1 | |
HELP_TEXT=" | |
git fuzzy-checkout | |
Check out branches quickly with the power of \`fzf\`. | |
Default view is local branches (HEADS). | |
Usage: | |
git fuzzy-checkout [OPTIONS] <QUERY> | |
OPTIONS: | |
-a : Show branches from heads and remotes | |
-r : Show branches from remotes | |
-t : Show branches from tags | |
-h : Show help | |
QUERY: | |
An optional query to immediately filter the ref list. | |
This is passed to \`fzf\` as the initial query, and can be changed. | |
Examples | |
git fuzzy-checkout # Show branches from heads (local) | |
git fuzzy-checkout LPS-12345 # Show branches that match the query 'LPS-12345': | |
git fuzzy-checkout -r # Show branches from remotes | |
git fuzzy-checkout -a # Show branches from heads and remotes | |
git fuzzy-checkout -t # Show branches from tags | |
" | |
### Determine whether to search local, remotes, or all | |
COMMAND_ALL="git for-each-ref refs/heads refs/remotes refs/tags" | |
COMMAND_HEADS="git for-each-ref refs/heads" | |
COMMAND_REMOTES="git for-each-ref refs/remotes" | |
COMMAND_TAGS="git for-each-ref refs/tags" | |
COMMAND="${COMMAND_HEADS}" | |
while getopts "ahrt" FLAGS; do | |
case $FLAGS in | |
a) COMMAND="${COMMAND_ALL}" ;; | |
h) echo "$HELP_TEXT" && exit 0 ;; | |
r) COMMAND="${COMMAND_REMOTES}" ;; | |
t) COMMAND="${COMMAND_TAGS}" ;; | |
*) exit 1 ;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
### Set up display | |
DELIMITER=" :: " | |
FZF_HEADER=""" | |
enter: check out branch | |
alt-space: toggle preview window (shows 'git log' of the selected branch) | |
alt-bspace: delete branch | |
alt-y: copy branch name to clipboard | |
alt-[a h r t]: show [ALL HEADS REMOTES TAGS] | |
""" | |
GIT_BRANCH_DISPLAY_FORMAT="%(committerdate:short)${DELIMITER}%(color:yellow)%(refname:short)%(color:reset)" | |
RELOAD_SUFFIX="--color=always --format='${GIT_BRANCH_DISPLAY_FORMAT}' --sort=-committerdate" | |
### Go | |
${COMMAND} \ | |
--color=always \ | |
--format="${GIT_BRANCH_DISPLAY_FORMAT}" \ | |
--sort=-committerdate \ | |
| | |
fzf \ | |
--ansi \ | |
\ | |
--bind="alt-space:toggle-preview" \ | |
--bind="enter:execute(git checkout {2})+accept" \ | |
--bind="alt-bspace:execute-silent(git branch -D {+2})+reload:${COMMAND} ${RELOAD_SUFFIX}" \ | |
--bind="alt-y:execute(printf {+2} | pbcopy)" \ | |
--bind="alt-l:execute(jack {+2})" \ | |
\ | |
--bind="alt-a:reload:${COMMAND_ALL} ${RELOAD_SUFFIX}" \ | |
--bind="alt-h:reload:${COMMAND_HEADS} ${RELOAD_SUFFIX}" \ | |
--bind="alt-r:reload:${COMMAND_REMOTES} ${RELOAD_SUFFIX}" \ | |
--bind="alt-t:reload:${COMMAND_TAGS} ${RELOAD_SUFFIX}" \ | |
\ | |
--delimiter="${DELIMITER}" \ | |
--exact \ | |
--header="${FZF_HEADER}" \ | |
--info="inline" \ | |
--multi \ | |
--nth=2 \ | |
--no-sort \ | |
--preview-window="down:70%:hidden" \ | |
--preview="git log -100 --color=always {2}" \ | |
--query "$*" \ | |
--reverse \ | |
; |
Author
drewbrokke
commented
Aug 3, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment