How do I name and retrieve a Git stash by name?

Question

How do I save/apply a stash with a name? I don't want to have to look up its index number in git stash list. I tried git stash save "my_stash_name", but that only changes the stash description, and the corresponding git apply "my_stash_name" doesn't work.

Answer

To save a stash with a message:

git stash push -m "my_stash_name"

Alternatively (deprecated since v2.16):

git stash save "my_stash_name"

To list stashes:

git stash list

All the stashes are stored in a stack.


To pop (i.e. apply and drop) the nth stash:

git stash pop stash@{n}

To pop (i.e. apply and drop) a stash by name is not possible with git stash pop (see footnote-1).


To apply the nth stash:

git stash apply stash@{n}

To apply a stash by name:

git stash apply stash^{/my_stash_name}

footnote-1:

  • See the man git-stash section regarding apply:

    Unlike pop, may be any commit that looks like a commit created by stash push or stash create.

  • Possible workaround (tested on git version 2.27 and 2.31):

    git stash pop $(git stash list --pretty='%gd %s'|grep "my_stash_name"|head -1|gawk '{print $1}')

Copy a git repo without history

How do I revert all local changes in Git managed project to previous state?