Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-commit

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: wp-content/w3tc-config/master.php 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).

Any ideas?

like image 913
Jo Sprague Avatar asked Apr 01 '13 14:04

Jo Sprague


People also ask

How do you resolve please commit your changes or stash them before you merge?

Most of cases the error Please, commit your changes or stash them before you can merge comes when you have made any changes to the local machine. After that, you must have pushed the changes to the Github remote repository. Now if anyone will pull all the changes from the remote then they will get the error.

Do I need to commit before merge?

Ideally, you'd like it to be clean before you attempt to merge in remote changes (remember, git pull = git fetch + git merge ). git commit is one way of accomplishing this, but it will alter your history--thereby polluting it if you're concerned about leaving your repo in a constantly working state.

What does it mean to stash your changes in git?

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.


1 Answers

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" 
  • Stash it.

    Stashing acts as a stack, where you can push changes, and you pop them in reverse order.

    To stash, type

    git stash 

    Do the merge, and then pull the stash:

    git stash pop 
  • Discard the local changes

    using git reset --hard
    or git checkout -t -f remote/branch

    Or: Discard local changes for a specific file

    using git checkout filename

like image 114
stdcall Avatar answered Oct 07 '22 06:10

stdcall