Can't pop git stash, 'Your local changes to the following files would be overwritten by merge'
Solution 1
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 2
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 3
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. :-)
Related videos on Youtube
Comments
-
fredley over 2 years
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 togit 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 andgit 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?
-
Wilfred Hughes over 7 yearsPossible duplicate of Cannot apply stash to working directory
-
iFreilicht over 6 yearsRelated when there are local changes: git stash -> merge stashed change with current changes
-
-
fredley over 10 yearsThis doesn't seem to work, I'm starting with a clean working directory with no untracked files, there's nothing to
mv
out of the way! -
torek over 10 yearsHm. Might be a bug in
git stash
(I've found one before) ... it would be interesting if you can reproduce this, especially with a small example. -
Joost over 7 yearsWhenever I do this I feel like I'm not using git properly.. Found no alternative though.
-
Taylor D. Edmiston over 7 yearsFor the record, doing this still gave the same errors for me even with a clean working directory.
-
Jamie M. over 7 yearsThis worked for me... emphasis on the
.
Can someone explain why this works? -
iFreilicht over 6 yearsI managed to find a second way: Instead of committing, run
git add <files>
for all files that would be overwritten. Then you canpop
, and then just unstage again withgit reset HEAD
. This has to be a bug of some sort. -
LastStar007 over 6 years@Joost Why not stash instead of commit, then turn around and drop the top of the stash stack?
git stash save "junk"
git stash drop
git stash pop
-
Raja over 6 yearsVery nice @iFreilicht, I have switched to your method instead!
-
Raja over 5 yearsLastStar007, we want to keep the current "junk", and pop what is in the stash as well.
-
Admin about 4 yearsFor the uninformed, the . is an alias for "all non ignored files recursively", not to be confused with *. But it's 5 years ago, so you know this now. @JamieM.
-
little_birdie almost 4 yearsThis is great, thanks. It happens.. too often.. that I stash changes because I need to do urgent work on another branch. Then I go back to the original branch and keep working without popping my stash... It's a mess. This totally solves that problem.
-
Raja almost 3 years@little_birdie Not sure if this helps, but I use a git-aware-prompt and when I check out a branch it will remind me whether there was a stash made on that branch, nudging me to pop again.
-
information_interchange almost 3 yearsRan into a merge conflict...now what
-
Milvintsiss over 2 yearsI had a clean directory too but I was facing the same error, for me this solution didn't worked. I tried a
git stash apply --index
and it worked, can't really explain what this arg do but as per documentation "attempt to recreate the index"