Search code examples
git-stashgit

Trying to reverse apply stash, git stash show and git apply giving errors


I'm following this git stashing guide but when I try to un-stash by using the command

$ git stash show -p stash@{0} | git apply -R

or

$ git stash show -p | git apply -R

I keep getting these errors

error: patch failed: app/scripts/app.js:20
error: app/scripts/app.js: patch does not apply
error: patch failed: app/views/main.html:34
error: app/views/main.html: patch does not apply

How do I get past this error?

When I do $git stash list it shows stash@{0}: WIP on my_branch: dc19ed5 My Commit


Solution

  • If you've modified the code after applying a stash you won't be able to reverse the applied stash in the manner suggested by that guide. This is because git can no longer apply the patch specified by the stash as the code doesn't look how it expects any more.

    You could fix up the patch output by git stash show -p stash@{0} manually, but unless the changes made since the patch was applied were very minor I wouldn't recommend it.

    These steps should allow you to get to the state you want, there may be a better way so I'll update this answer if I think of it:

    1. Stash your code in the state it's currently in
    2. Apply the previous stash and commit
    3. Apply the new stash and commit (there may be some merge conflicts which you'll have to fix manually)
    4. Revert the first commit

    This should leave you with a commit that only has your new changes. You may want to do this on a branch.