3

I am using SmartGit in Windows. Today I needed to do a hard reset back a couple commits. SmartGit automatically made a stash for me of my modified files.

After the reset, it tried to apply the stash:

$ git.exe stash apply --index stash@{0}

But it gave me:

Apply Stash: stash failed (return code 1)

I can view the stash contents and see all my code, but I can't apply the stash. How do I fix this?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370

3 Answers3

6

Just do git stash pop. It will get you your stashed changes in the working tree. (pop considering you want stash@{0}.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Mudassir Razvi
  • 1,783
  • 12
  • 33
4

Can you export it to a patch, and then try to apply manually?

git stash show -p > my_stash.patch
acanby
  • 2,936
  • 24
  • 26
4

There are several things you can do:

  1. git apply --index tries to preserve the staged vs unstaged setup of the stash you're applying. Often this is "too hard" but applying without --index, allowing all the changes to become unstaged, is possible. If you don't need to maintain the separation, that may "just work".

  2. As acanby suggested, you can turn the stash into a patch. Note that git stash show shows only the unstaged part of the stash, so this is somewhat like not using --index as well.

  3. If all else fails, you can turn your saved stash into its own (new) branch. This will resurrect all the commits you reset-away, but only on that new branch. In essence,1 what git stash branch newbranch does is:

    • turn the saved index in the stash into an ordinary commit
    • whose parent is the commit that was active (was the HEAD commit) when you did the git stash save step (and its parent is its same old parent, etc);
    • make the index-commit tip of the branch newbranch, which is a newly created branch; and
    • leave the work-tree and (if saved as part of the stash) untracked/all files ready for you to commit on the new branch you're now on.

1It's really "fact" more than "essence": this is how git stash branch works simply because it leaves the i commit of the stash-bag in place and makes a new branch-name pointing to it.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775