Can't pop git stash, 'Your local changes to the following files would be overwritten by merge'
GitGit StashGit Problem Overview
So I had a load of changes and some untracked files. I needed to tweak something, so I used git stash -u
, modified a couple of things, committed those changes, pushed them, and then tried to git stash pop
.
Because I'd modified a couple of files that I'd stashed, I got the following message:
error: Your local changes to the following files would be overwritten by merge:
file_1.py
file_2.py
Please, commit your changes or stash them before you can merge.
Aborting
This seems odd, I had committed all new changes, my checkout was clean when I ran the command.
It seems the git stash pop
operation un-stashed half of my changes and the untracked files, but if I try and git stash pop
again I get output like:
some_file.html already exists, no checkout
some_other_file.html already exists, no checkout
yet_another_file.html already exists, no checkout
Could not restore untracked files from stash
git stash show
still shows a list of my stashed changes, but I'm at a loss as to what I do now.
How can I get myself unstuck?
Git Solutions
Solution 1 - Git
For those who do have un-committed work, and want to pop their stash without losing that work, here is a way (with thanks to @iFreilicht):
-
Temporarily stage any uncommitted changes:
git add -u .
-
Now you can apply your stash without git complaining (hopefully):
git stash pop
-
Now unstage everything, but leave the files as they are now:
git reset
If step 2 couldn't patch cleanly due to conflicting changes, then you will need to resolve the conflicts manually. git diff
should help you find them.
Solution 2 - Git
I got around this, I think it must have been some kind of bug, as my working directory was clean and up to date.
I ran git checkout .
and after that git stash apply
worked fine, I got everything back no problems at all. I'd be interested to work out what actually caused it to fail though.
Solution 3 - Git
The stash that was made with -u
needs to have the untracked files cleaned away before being apply
-ed (and pop
is just apply
+drop
).
Out of general paranoia I'd mv
the untracked files somewhere safe, then git stash apply
, check everything carefully, and git stash drop
once I'm sure I have it all correct. :-)
Solution 4 - Git
None of these solutions worked for me. I was using git stash index command to restore a specific stash id. So, I ended up doing a commit of my local changes to local repo. Then git stash index worked for me. And finally I rolled back my commit using git reset (with keep changes). Problem solved.