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

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

You cant 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).

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

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 itll 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 dont 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 dont care about your local changes, try to reset it to HEAD (original state), e.g.

git reset HEAD --hard

If above wont help, it may be rules in your git normalization file (.gitattributes) so its better to commit what it says. Or your file system doesnt support permissions, so youve to disable filemode in your git config.

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

Leave a Reply

Your email address will not be published.