151

I had to quickly switch git branches, so I ran git stash, but I had to run it again because one of my files needed editing.

So I've run git stash twice, and I'm ready to go back to editing my files. I ran git stash apply but I'm not convinced that all of the files I had stashed were unstashed. Is there anything I can do? Any way to check?

When I run git stash show, I just see the last of my two git stashes.

Is there anyway to show all git stashes?

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189

4 Answers4

246

You can get a list of all stashes with

git stash list

which will show you something like

stash@{0}: WIP on dev: 1f6f8bb Commit message A
stash@{1}: WIP on master: 50cf63b Commit message B

If you made two stashes, then just call git stash pop twice. As opposed to git stash apply, pop applies and removes the latest stash.

You can also reference a specific stash, e.g.

git stash show stash@{1}

or

git stash apply stash@{1}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 32
    If you want to `git stash pop` twice because you want both stashes in the same commit but you encounter "error: Your local changes to the following files would be overwritten by merge:" on your 2nd `git stash pop`, then you can: 1) `git stash pop`, 2) `git add .`, and 3) `git stash pop`. – gabe Mar 05 '15 at 16:37
  • This helped me. I needed to do "git stash" and then "git stash --all" to stash some new untracked files. Doing git stash pop twice was not working. I only got the later stash back. – Leopold Kristjansson Oct 09 '19 at 20:27
11

I came across this situation, I did two stashes and git stash pop just unstashed last stash. So I did

git stash list

git stash pop stash@{1}

This unstashed my first stash and I could see all my changes back!

vallentin
  • 23,478
  • 6
  • 59
  • 81
Waaheeda
  • 394
  • 3
  • 14
6

You asked a few different questions in post, and other respondents gave good answers to some of them. The one that seems most important but hasn't been answered is this:

>>I'm not convinced that all of the files I had stashed were unstashed. Is there anything I can do? Any way to check?

Compare stash to local tree

I think what you want to do is compare the stash to your local working tree. You can put the -p switch on the stash command and you're good:

git stash show -p

If there's a particular one you're after, just use its git stash name or id from the stash list:

git stash show -p stash@{3}

Maybe use the diff?

If you're really interested in pushing your git skills, you could always go for a diff. To see the difference between what's in the stash and whats checked into the HEAD on the master branch the following diff could be used:

git diff stash@ master

Another neat command to show you changes for elements in the stash history that might come in handy is --stat:

git stash list --stat

But I think the simple answer is the right answer. Just use the -p switch and you'll likely see if the stash you shelved has been popped back.

git stash show -p stash@{3}
vallentin
  • 23,478
  • 6
  • 59
  • 81
Cameron McKenzie
  • 3,684
  • 32
  • 28
1

Just in case :

If you are on VSCode Terminal and try git stash apply stash@{1} you can have an error: unknow switch `e'

You can do git stash apply 'stash@{1}' with quotes