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

44,338

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

  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.

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

Share:
44,338

Related videos on Youtube

fredley
Author by

fredley

Incomplete project factory.

Updated on September 03, 2021

Comments

  • fredley
    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 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?

  • fredley
    fredley over 10 years
    This 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
    torek over 10 years
    Hm. 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
    Joost over 7 years
    Whenever I do this I feel like I'm not using git properly.. Found no alternative though.
  • Taylor D. Edmiston
    Taylor D. Edmiston over 7 years
    For the record, doing this still gave the same errors for me even with a clean working directory.
  • Jamie M.
    Jamie M. over 7 years
    This worked for me... emphasis on the . Can someone explain why this works?
  • iFreilicht
    iFreilicht over 6 years
    I managed to find a second way: Instead of committing, run git add <files> for all files that would be overwritten. Then you can pop, and then just unstage again with git reset HEAD. This has to be a bug of some sort.
  • LastStar007
    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
    Raja over 6 years
    Very nice @iFreilicht, I have switched to your method instead!
  • Raja
    Raja over 5 years
    LastStar007, we want to keep the current "junk", and pop what is in the stash as well.
  • Admin
    Admin about 4 years
    For 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
    little_birdie almost 4 years
    This 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
    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
    information_interchange almost 3 years
    Ran into a merge conflict...now what
  • Milvintsiss
    Milvintsiss over 2 years
    I 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"