Suan Suan - 1 year ago 272
Git Question

How to name and retrieve a stash by name in git?

I was always under the impression that you could give a stash a name by doing

git stash save stashname
, which you could later on apply by doing
git stash apply stashname
. But it seems that in this case all that happens is that
will be used as the stash description.

Is there no way to actually name a stash? If not, what would you recommend to achieve equivalent functionality? Essentially I have a small stash which I would periodically like to apply, but don't want to always have to hunt in
git stash list
what its actual stash number is.

Answer Source

You can actually find the stash by name using git's regular expression syntax for addressing objects:


For example, when saving your stash with a save name:

git stash save "guacamole sauce WIP"

... you can use a regular expression to address that stash:

git stash apply stash^{/guacamo}

This will apply the youngest stash that matches the regular expression guacamo. That way, you don't have to know what number the stash is at in the stack, you just have to know its name. There is no terser syntax for this, but you can create an alias in your .gitconfig file:

sshow = "!f() { git stash show stash^{/$*} -p; }; f"
sapply = "!f() { git stash apply stash^{/$*}; }; f"

You can then use git sapply <regex> to apply that stash (without dropping).
You can then use git sshow <regex> to show: files changed, insertions, and deletions

EDIT: Props to this StackOverflow answer on how to use bash arguments in git aliases.

EDIT 2: This answer used to contain drop and list aliases, but I've since removed them, since drop requires the stash@{n} syntax while list didn't filter the stashes at all. If anyone knows how to resolve a stash SHA-1 hash to a stash ref, then I could implement the other commands as well.

EDIT 3: Per isyi's suggestion I've added a patch flag to show what the contents of the stash are when showing one.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download