How do I resolve git saying "Commit your changes or stash them before you can merge"?
I made some updates on my local machine, pushed them to a remote repository, and now I'm trying to pull the changes to the server and I get the message;
error: Your local changes to the following files would be overwritten by merge:
Please, commit your changes or stash them before you can merge.
So I ran,
git checkout -- wp-content/w3tc-config/master.php
and tried again and I get the same message. I'm assuming that
w3tc changed something in the config file on the server. I don't care whether the local copy or remote copy goes on the server (I suppose the remote one is best), I just want to be able to merge the rest of my changes (plugin updates).
You can't merge with local modifications. Git protects you from losing potentially important changes.
You have three options:
Commit the change using
git commit -m "My message"
Stashing acts as a stack, where you can push changes, and you pop them in reverse order.
To stash, type
Do the merge, and then pull the stash:
git stash pop
Discard the local changes
git reset --hard
git checkout -t -f remote/branch
Or: Discard local changes for a specific file
git checkout filename
Read more... Read less...
git stash git pull <remote name> <remote branch name> (or) switch branch git stash apply --index
The first command stores your changes temporarily in the stash and removes them from the working directory.
The second command switches branches.
The third command restores the changes which you have stored in the stash (the
--index option is useful to make sure that staged files are still staged).
You can try one of the following methods:
For simple changes try rebasing on top of it while pulling the changes, e.g.
git pull origin master -r
So it'll apply your current branch on top of the upstream branch after fetching.
This is equivalent to:
rebase origin/master git commands.
This is a potentially dangerous mode of operation. It rewrites history, which does not bode well when you published that history already. Do not use this option unless you have read
If you don't care about your local changes, you can switch to other branch temporary (with force), and switch it back, e.g.
git checkout origin/master -f git checkout master -f
If you don't care about your local changes, try to reset it to HEAD (original state), e.g.
git reset HEAD --hard
If above won't help, it may be rules in your git normalization file (
.gitattributes) so it's better to commit what it says. Or your file system doesn't support permissions, so you've to disable
filemode in your git config.
git stash save ""
and try pull again
So the situation that I ran into was the following:
error: Your local changes to the following files would be overwritten by merge: wp-content/w3tc-config/master.php Please, commit your changes or stash them before you can merge.
except, right before that, was remote: so actually this:
remote: error: Your local changes to the following files would be overwritten by merge: some/file.ext Please, commit your changes or stash them before you can merge.
What was happening was (I think, not 100% positive) the git post receive hook was starting to run and screwing up due to movement changes in the remote server repository, which in theory, shouldn't have been touched.
So what I ended up doing by tracing through the post-receive hook and finding this, was having to go to the remote repository on the server, and there was the change (which wasn't on my local repository, which, in fact, said that it matched, no changes, nothing to commit, up to date, etc.) So while on the local, there were no changes, on the server, I then did a
git checkout -- some/file.ext and then the local and remote repositories actually matched and I could continue to work, and deploy. Not entirely sure how this situation occurred, though a couple dozen developers plus IT changes may had something to do with it.
WARNING: This will delete untracked files, so it's not a great answer to this question.
In my case, I didn't want to keep the files, so this worked for me:
Git 2.11 and newer:
git clean -d -fx .
git clean -d -fx ""
-x means ignored files are also removed as well as files unknown to git.
-d means remove untracked directories in addition to untracked files.
-f is required to force it to run.