Skip to content

Instantly share code, notes, and snippets.

@junegunn
Created July 21, 2016 05:03
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save junegunn/a563d9e3e07fd721d618562762ec619d to your computer and use it in GitHub Desktop.
Save junegunn/a563d9e3e07fd721d618562762ec619d to your computer and use it in GitHub Desktop.
gstash
gstash() {
local out k reflog
out=(
$(git stash list --pretty='%C(yellow)%gd %>(14)%Cgreen%cr %C(blue)%gs' |
fzf --ansi --no-sort --header='enter:show, ctrl-d:diff, ctrl-o:pop, ctrl-y:apply, ctrl-x:drop' \
--preview='git stash show --color=always -p $(cut -d" " -f1 <<< {}) | head -'$LINES \
--preview-window=down:50% --reverse \
--bind='enter:execute(git stash show --color=always -p $(cut -d" " -f1 <<< {}) | less -r > /dev/tty)' \
--bind='ctrl-d:execute(git diff --color=always $(cut -d" " -f1 <<< {}) | less -r > /dev/tty)' \
--expect=ctrl-o,ctrl-y,ctrl-x))
k=${out[0]}
reflog=${out[1]}
[ -n "$reflog" ] && case "$k" in
ctrl-o) git stash pop $reflog ;;
ctrl-y) git stash apply $reflog ;;
ctrl-x) git stash drop $reflog ;;
esac
}
@sangwook
Copy link

👍

@OliverJAsh
Copy link

This is amazing. Thank you!

I had to make a minor fix:

-  k=${out[0]}
-  reflog=${out[1]}
+  k=${out[1]}
+  reflog=${out[2]}

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