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

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


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:

rebase

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: checkout master, fetch and 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 git-rebase(1) carefully.


checkout

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

reset

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.

Related: How do I force "git pull" to overwrite local files?

Tags:

Git