Search code examples
gitgit-stash

Why local changes to the following files would be overwritten by merge?


I have changes to Makefile in different places.

screenshot: "git diff" showing local changes

screenshot: "git stash show -p" showing different changes

Why I still get error?

$ git stash pop
error: Your local changes to the following files would be overwritten by merge:
    Makefile
Please commit your changes or stash them before you merge.
Aborting
The stash entry is kept in case you need it again.

My changes are not intersect. So I expect they should be merged without any problem.


Solution

  • There are technical issues, which prevent git stash pop to use the index as freely as other commands (since it actually modifies the index itself), and to avoid a complicated logic around the stash apply mechanism, to this point, git stash pop simply refuses to apply changes on an "unclean" file.


    The simple way around this is to create a commit with said files :

    git add Makefile
    git commit -m "wip"
    git stash pop
    
    # ... fix stuff if needed ...
    
    # after stash application : you can jump one commit backwards
    git reset --soft HEAD^   # if you want to keep stuff in the index
    git reset HEAD^          # if you can reset the index too