Categories
git git-stash

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

2053

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 stashname 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.

6

  • 154

    git stash push -m stashname is the current syntax. git stash save stashname has been deprecated.

    Apr 25, 2018 at 18:08


  • 3

    git stash push -m stashname doesn’t work in 2.8.0.windows.1.

    – Jac

    Jun 10, 2018 at 10:29

  • 1

    Git for Windows 2.26.0 is released a few days ago. Maybe it’s now fixed. github.com/git-for-windows/git/releases/tag/v2.26.0.windows.1

    Apr 6, 2020 at 13:18

  • Actually I think if your stash list is longer than three items, you should rethink your workflow.

    – U. Windl

    Apr 8, 2021 at 6:40

  • 1

    Simple custom git command (link to gist). Usage: git bottle name and then later git apply name.patch, see my answer if you want a fuller explanation and usage samples.

    – GrayedFox

    Aug 31, 2021 at 12:17


678

git stash save is deprecated as of 2.15.x/2.16, instead you can use git stash push -m "message"

You can use it like this:

git stash push -m "message"

where “message” is your note for that stash.

In order to retrieve the stash you can use: git stash list. This will output a list like this, for example:

[email protected]{0}: On develop: perf-spike
[email protected]{1}: On develop: node v10

Then you simply use apply giving it the [email protected]{index}:

git stash apply 1

References
git stash man page

10

  • 11

    docs showing push rather than save syntax: git stash push

    Apr 25, 2018 at 18:04

  • 40

    This is the real answer. Unfortunately, there are a ton of old answers above it.

    – mas

    Dec 12, 2018 at 19:37

  • 1

    For more on the newer git stash push: stackoverflow.com/a/47231547/6309

    – VonC

    Jan 28, 2019 at 15:03

  • 6

    FWIW: When runninggit stash apply [email protected]{1} in Powershell you will get a error: unknown switch 'e' back. Instead use git stash apply --index 1 or git stash apply '[email protected]{1}' or escape } and { with a backtick `.

    – LosManos

    May 26, 2020 at 6:59


  • 4

    Why does git choose an awkward naming style like [email protected]{1}? This is really hard to type on command line. If we can type something like git stash show -p @1 would be much easier…

    – zerox

    Aug 10, 2020 at 6:49

200

If you are just looking for a lightweight way to save some or all of your current working copy changes and then reapply them later at will, consider a patch file:

# save your working copy changes
git diff > some.patch

# re-apply it later
git apply some.patch

Every now and then I wonder if I should be using stashes for this and then I see things like the insanity above and I’m content with what I’m doing 🙂

5

  • 7

    This is it! Thank you. I have also updated my .gitignore to ignore .patch files and I am all set to have as many patches as I want.

    – LINGS

    Jun 24, 2019 at 14:27

  • 4

    I can see the intent behind the question, which is to have apply some local changes every time you take out a branch from master and not commit them. So, perhaps the question should have been corrected and this answer should have been accepted as the solution. Simple as well.

    Feb 18, 2020 at 10:13


  • 2

    Nice alternative to stash

    – isapir

    Aug 20, 2020 at 18:06

  • I think we may want to augment this answer with a git apply --3way some.patch. This is more similar to the traditional git stash apply approach. Otherwise, conflicts can cause the patch apply to fail

    Nov 13, 2020 at 20:46


  • 3

    I don’t believe this does new files.

    May 18, 2021 at 5:33