Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't pop git stash, 'Your local changes to the following files would be overwritten by merge'

Tags:

git

git-stash

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?

like image 388
fredley Avatar asked Nov 12 '13 18:11

fredley


People also ask

How do I resolve git saying commit your changes or stash them before you can merge?

The “commit your changes or stash them before you can merge” error is raised when you try to pull code from a remote repository that conflicts with a local change you have made to a repository. To solve this error, either commit your change to the repository, discard your change, or stash your change for later.

How do you fix error your local changes to the following files would be overwritten by checkout?

You can fix this issue by either stashing your changes for later or adding them to a commit.

Does git stash pop overwrite?

When the pop command runs, it's expected that files from the stash will overwrite the contents of the files in the local working tree, and the updated files will be staged in the git index.


2 Answers

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):

  1. Temporarily stage any uncommitted changes:

     git add -u . 
  2. Now you can apply your stash without git complaining (hopefully):

     git stash pop 
  3. 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.

like image 179
joeytwiddle Avatar answered Oct 13 '22 04:10

joeytwiddle


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.

like image 44
fredley Avatar answered Oct 13 '22 05:10

fredley