I am trying to apply changes I stashed earlier with git stash pop
and get the message:
Cannot apply to a dirty working tree, please stage your c
I do it in this way:
git add -A
git stash apply
and then (optionaly):
git reset
You can do this without having to stash your current changes by exporting the stash you want as a patch file and manually applying it.
For example, say you want to apply stash@{0} to a dirty tree:
Export stash@{0} as a patch:
git stash show -p stash@{0} > Stash0.patch
Manually apply the changes:
git apply Stash0.patch
If the second step fails, you will have to edit the Stash0.patch file to fix any errors and then try git apply again.
I was unable to get most of these to work; for some reason it always thinks I have local changes to a file. I can't apply a stash, patches won't apply, checkout
and reset --hard
fail. What finally worked was saving the stash as a branch with git stash branch tempbranchname
, and then doing a normal branch merge: git checkout master
and git merge tempbranchname
.
From http://git-scm.com/book/en/Git-Tools-Stashing :
If you want an easier way to test the stashed changes again, you can run git stash branch, which creates a new branch for you, checks out the commit you were on when you stashed your work, reapplies your work there, and then drops the stash if it applies successfully
None of these answers actually work if you find yourself in this situation as I did today. Regardless of how many git reset --hard
's I did, it got me nowhere. My answer (not official by any means was):
git reflog --all
You have files that have been modified but not committed. Either:
git reset --hard HEAD (to bring everything back to HEAD)
or, if you want to save your changes:
git checkout -b new_branch
git add ...
git commit
git checkout -b old_branch
git stash pop