0

I've been working on a project for a while and used git from the beginning. After a few commits, I made a change to a file which I realised was unnecessary. In an attempt to discard those changes and return to the state of the latest commit, I used:

git stash save --keep-index

which gave me the output:

Saved working directory and index state WIP on master: cc91857 fixed animation bug HEAD is now at cc91857 fixed animation bug

and dropped the stash by typing

git stash drop 

Output:

Dropped refs/stash@{0} (dfb694b90757d8d25318b09da4f5dad2f3be20a6)

However, instead of changing the edited file to the state of the last commit, it was changed to the state of the initial commit, although my head remained at the same commit. To my horror, I have discovered that the file that contained changes I originally wanted to drop now appears to have remained unchanged between the first commit and the most recent one. Other files are not affected.

Is there any way to reset the file to the state of my last recent commit?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
JonaSc
  • 195
  • 1
  • 2
  • 8
  • possible duplicate. http://stackoverflow.com/questions/4114095/revert-to-a-previous-git-commit – dsharew Dec 12 '14 at 14:35
  • I don't see how this is a duplicate – JonaSc Dec 12 '14 at 15:23
  • The correct option is `--keep-index` (not the hyphen). Are you sure you ran `git stash save --keep index` (without hyphen) or is it a typo? – jub0bs Dec 12 '14 at 15:38
  • That was a typo, edited it. Thanks – JonaSc Dec 12 '14 at 15:39
  • I've edited your question slightly, for clarity; feel free to change it if you think my edit is inaccurate. Because the `git stash` commands you report using cannot affect your commits, it looks like the file in question really hasn't changed since the first commit. Perhaps the commit you're trying to get back to is on a different branch to the one you're on right now.. – jub0bs Dec 12 '14 at 15:57
  • The accepted answer to http://stackoverflow.com/questions/89332/recover-dropped-stash-in-git fixed the issue. – JonaSc Dec 12 '14 at 16:26
  • @JonaSc Cool. Should we close this question as a duplicate of http://stackoverflow.com/questions/89332/recover-dropped-stash-in-git, then? – jub0bs Dec 12 '14 at 16:35

1 Answers1

0

To recover the stash,

touch .git/refs/stash     # make sure stash reflog exists, see update-ref docs
git update-ref -m "$(git show -s --format:%s dfb694b9)" refs/stash dfb694b9
git checkout master       # (where you took the stash originally)
git stash apply
jthill
  • 55,082
  • 5
  • 77
  • 137