Howto remove/resolve git message “Commit your changes or stash them before you can merge” on Linux Systems

undefined

In this mini post I’ll show you how to  solve this git error message “Commit your changes or stash them before you can merge” which appears when you try to pull the latest code from your git repository and did some “not committed” modifications to your code/repository on your local machine.

I was trying to pull the latest code using this command:

$ git pull origin master

And the pulling process failed. Here’s I show you how to solve this.

We’ve four options to solve this error and pull the latest code from our repository, let’s start.

Option 1: Committing you local changes.

Use this option if your local changes are important and you want to keep them in your remote repository. To commit your local changes run the following command:

$ git commit -m "My message"

Now after committing your changes, run the pull command and the error message is gone.

Option 2: Stash IT

Stashing acts as a stack, where you can push changes temporarily and discard those changes from working directory, then you can pull / switch branches, and finally restore the important changes which you have discarded temporarily. (–index option is useful for those files are not staged are being upstaged)

To stash type:

$ git stash

Now, pull your remote repository or switch branch:

$ git pull <remote name> <remote branch name> (or) switch branch

After you did the merge, pull the stash:

$ git stash pop

Option 3: Discard the local changes

Use this option if your local changes are NOT important and you can remove your local changes without harm yourself. To discard your local changes run any of the following command:

$ git reset --hard

OR

$ git checkout -t -f remote/branch

Now, pull you repository and it’ll successfully merged.

Option 4: Discard the local changes for certain file/files

This is a special case, if you want to discard your local changes for certain file, run the following command:

$ git checkout <filename>

Now, pull you repository and it’ll successfully merged.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *