Dave Heq Dave Heq - 1 month ago 6x
Git Question

Alias: `git pop last`, for `git stash pop [last stash on the list]`

Is this even possible? I want an easier command to

git stash pop stash@{13}
would simply be
meaning "the last stash on the list" or "the oldest stash".

I know I can create an alias of
git pop
git stash pop
(which I could use like
git pop stash@{13}
), but I'd like something simpler like
git pop last
. Would I need to write my own script or is there a way to do it just with git or alias? I use Windows primarily but sometimes Linux.


Building off the hints provided by @torek, this should get you the ref of the stash you want:

git reflog stash | tail -n 1 | cut -d ' ' -f 2 | cut -d ':' -f 1

Thus, you can set your alias like this:

git config alias.pop-last "! git stash pop $(git reflog stash | tail -n 1 | cut -d ' ' -f 2 | cut -d ':' -f 1)"

I've tested and this works in the Git Bash prompt on Windows. (It should also work in Linux.)