587

I just did a stash in a project that I haven't commit. Is there a way to go back to the state before I stashed? How could I do this? I've closed the terminal and my laptop is shut down. I've done some researched and it seems there's no way to do this.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
adit
  • 32,574
  • 72
  • 229
  • 373
  • Related post [here](https://stackoverflow.com/q/19003009/465053). – RBT Sep 05 '17 at 06:25
  • To clarify, because I do think this is confusing to people new-to-version-control: The stash is changes made to files. So if you closed your terminal, or shutdown your system, they would not have additional effects on your situation. You simply need to terminal-shell to the same place (in the git repository), and then sort out your next step in handling the stash. – benc May 18 '22 at 20:15

3 Answers3

1097

You can just run:

git stash pop

and it will unstash your changes.

If you want to preserve the state of files (staged vs. working), use

git stash apply --index
starwed
  • 2,536
  • 2
  • 25
  • 39
ziad-saab
  • 19,139
  • 3
  • 36
  • 31
  • 17
    Well, not quite. It will not reset what is in the staging directory, so you will have to restage all that (which can be annoying if you had just done a complex git add -p – Nick Jan 02 '13 at 17:31
  • Didn't work for me. I got an error: `mymodule/MyClass.java: needs merge - unable to refresh index`. Avoiding a manual merge was precisely my goal when using `git stash pop`... – TanguyP Sep 18 '15 at 15:10
  • `git config alias.unstash stash apply --index` will allow you to use `git unstash` and have it do what you think it should do. – beyarkay Jun 27 '23 at 13:51
201

git stash list to list your stashed changes.

git stash show to see what n is in the below commands.

git stash apply to apply the most recent stash.

git stash apply stash@{n} to apply an older stash.

https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning

OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
  • 1
    also "git stash show" after "git stash list" just to round out this excellent answer and better identify what to replace n with. Turns out I have stuff stashed from files that don't exist any longer! – JimLohse Jan 15 '16 at 22:53
23

This will also restore the staging directory:

git stash apply --index
wisbucky
  • 33,218
  • 10
  • 150
  • 101