811

I have 2 branches: master | design

Working in design I did a stash and switched to master, made some adjustments. Switched back to design and did a stash apply only to lose all my changes in the design branch.

I am hoping all my work is within a stash as I have not cleared or removed these.

If I do a stash list I get 4 results:

stash@{0}: WIP on design: f2c0c72... Adjust Password Recover Email
stash@{1}: WIP on design: f2c0c72... Adjust Password Recover Email
stash@{2}: WIP on design: eb65635... Email Adjust
stash@{3}: WIP on design: eb65635... Email Adjust

If I try git stash apply f2c0c72 I am getting an error:

fatal: Needed a single revision
f2c0c72: no valid stashed state found

How can I apply a specific stash?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Lee
  • 20,034
  • 23
  • 75
  • 102
  • 50
    Note that you have now (Q4 2016, Git 2.11) the syntax `git stash apply 0` (instead of `git stash apply stash@{0}`). See [my answer here](http://stackoverflow.com/a/40543440/6309). – VonC Nov 11 '16 at 08:17

8 Answers8

1387

The keys into the stash are actually the stash@{n} items on the left. So try:

git stash apply stash@{0}

(note that in some shells you need to quote "stash@{0}", like zsh, fish and powershell).

Since version 2.11, it's pretty easy, you can use the N stack number instead of using stash@{n}. So now instead of using:

git stash apply "stash@{n}"

You can type:

git stash apply n

To get list of stashes:

git stash list

In fact stash@{0} is a revision in git that you can switch to... but git stash apply ... should figure out how to DTRT to apply it to your current location.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
araqnid
  • 127,052
  • 24
  • 157
  • 134
293

To apply a stash and remove it from the stash list, run:

git stash pop stash@{n}

To apply a stash and keep it in the stash cache, run:

git stash apply stash@{n}
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
  • 8
    This is great, thanks for distinguishing between the two options. I just tried this out and it appears that if you pop a stash from branch-a onto branch-b, the stash will still remain in the stash cache. I guess that would be so you still have the option of pop/applying the stash to branch-a at a future date. Really hope that made sense. Handy! – longda Mar 27 '13 at 22:34
  • 47
    For PowerShell: `git stash pop "stash@{n}"` – ankitjaininfo Apr 04 '14 at 06:56
  • 1
    PowerShell's happy with just curly braces in quotes/single-quotes: `git stash apply stash@"{n}"` – m1kael Jun 22 '16 at 06:37
  • PowerShell will also accept `git stash apply stash@\`{n\`}` (note the backticks before the braces). – Ian Kemp Nov 07 '16 at 12:22
77

Since version 2.11, it's pretty easy, you can use the N stack number instead of saying "stash@{n}". So now instead of using:

git stash apply "stash@{n}"

You can type:

git stash apply n

For example, in your list:

stash@{0}: WIP on design: f2c0c72... Adjust Password Recover Email
stash@{1}: WIP on design: f2c0c72... Adjust Password Recover Email
stash@{2}: WIP on design: eb65635... Email Adjust
stash@{3}: WIP on design: eb65635... Email Adjust

If you want to apply stash@{1} you could type:

git stash apply 1

Otherwise, you can use it even if you have some changes in your directory since 1.7.5.1, but you must be sure the stash won't overwrite your working directory changes if it does you'll get an error:

error: Your local changes to the following files would be overwritten by merge:
        file
Please commit your changes or stash them before you merge.

In versions prior to 1.7.5.1, it refused to work if there was a change in the working directory.


Git release notes:

The user always has to say "stash@{$N}" when naming a single element in the default location of the stash, i.e. reflogs in refs/stash. The "git stash" command learned to accept "git stash apply 4" as a short-hand for "git stash apply stash@{4}"

git stash apply" used to refuse to work if there was any change in the working tree, even when the change did not overlap with the change the stash recorded

Pallas
  • 1,499
  • 5
  • 25
  • 57
Pau
  • 14,917
  • 14
  • 67
  • 94
46

If one is on a Windows machine and in PowerShell, one needs to quote the argument such as:

git stash apply "stash@{0}"

...or to apply the changes and remove from the stash:

git stash pop "stash@{0}"

Otherwise without the quotes you might get this error:

fatal: ambiguous argument 'stash@': unknown revision or path not in the working tree.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
jterry
  • 6,209
  • 2
  • 30
  • 36
  • Apparently this is how you have to do it when using fish shell on Mac OSX 10.11 as well. – lps Nov 11 '15 at 20:27
29
git stash list 

List will show all stashed items eg:stash@{0}:,stash@{1}:,..,stash@{n}:

Then select the number n which denotes stash@{n}:

git stash apply n 

for example:

git stash apply 1 

will apply that particular stashed changes to the current branch

dejanualex
  • 3,872
  • 6
  • 22
  • 37
19

If you have more stashes I recommend you to check before you will apply them. Use:

git stash show -p

to see the last stash in detail. If you have more stashes and you want a specific one then add a number of stash at the end:

git stash show 1

Then when you find the right stash apply it by typing:

git stash apply 1

Again the number is an example without number you will apply the last one.

Jan Sandr
  • 201
  • 2
  • 4
16

To view your recent work and what branch it happened on run

git stash list

then select the stash to apply and use only number:

git stash apply n

Where n (in the above sample) is that number corresponding to the Work In Progress.

JGallardo
  • 11,074
  • 10
  • 82
  • 96
panthari
  • 171
  • 1
  • 6
1

You need to quote the string, because your shell is eating the content of the {} as an expansion. So use git stash apply 'stash@{index}'. Alternatively you can use the SHA of the stash, or next time when you apply it, you can name the stash yourself.

Gurmeet Singh
  • 199
  • 10