After adding some changes to the index with git add -p, I then issued a git stash but forgot to add --keep-index. Then I stupidly did a git stash pop, and all my changes to the index were gone. Is there a way to recover the index to the state before the git stash?
-
Stashing usually saves both the index and the changes in the working directory separately, so popping the stash should restore the index as well? – poke Jan 10 '13 at 18:11
-
@poke That's not what happened unfortunately, the index was emptied by git stash and remained so after the git stash pop. Maybe if I had done a git stash pop --index instead it would have been restored, but I didn't :/ – Watcom Jan 10 '13 at 18:46
-
@poke first part correct, second part incorrect. `--index` must be added to the `git stash apply|pop` for that. – maliayas Nov 30 '13 at 22:37
2 Answers
When you've just done git stash pop
, the last line in the output is:
Dropped refs/stash@{0} (ca82a6dff817ec66f44342007202690a93763949)
If you've lost it, see How to recover a dropped stash in Git? to find the commit hash.
Once you have the hash, either:
Drop all current changes (applied stash):
git reset --hard
And reapply the stash using its id, this time with index:
git stash apply ca82a6d --index
Reset only the index, the point here is that the index is saved as a second parent of the stash:
git reset ca82a6d^2 .
Notice the dot at the end. If you don't specify it, it'll also move HEAD to the index (the index will appear as a commit). In this case run
git reset --soft HEAD@{1}
to return the HEAD to its previous position.

- 23,260
- 9
- 113
- 101
This will do the job:
git stash apply --index
Edit: Considering the index is already lost, you should run a git fsck --unreachable
and inspect the latest commits it gives. You must be able to see your lost index there. After that you can git cherry-pick
it.

- 2,674
- 1
- 18
- 17
-
Neat! Stashes do keep a separate state of index and working directory. I had no idea of this. – Maic López Sáenz Dec 02 '13 at 22:27
-
1