GIT how do i resolve git saying commit your changes or stash them before you can me

July 14, 2021 . 4 MIN READ

https://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me

 

https://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me

 

Discard the local changes

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

 

 

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?

git

Share

Improve this question

Follow

edited Feb 1 ’19 at 9:39

 

CopsOnRoad

106k3030 gold badges353353 silver badges247247 bronze badges

asked Apr 1 ’13 at 14:11

 

Jo Sprague

13.4k99 gold badges3737 silver badges5757 bronze badges

  • Possible duplicate of How to ignore error on git pull about my local changes would be overwritten by merge?– hestellezg Jul 14 ’17 at 18:48
  • 13

This is a more explicit question with more details and a better answer. I think it’s valuable to keep this one around. Yes, the other one was technically asked first, but removing this one would make it more difficult for people to find the answers they’re looking for. – Jo Sprague Jul 17 ’17 at 11:19

Add a comment

18 Answers

ActiveOldestVotes

1514

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

Share

Improve this answer

Follow

edited Jun 26 ’18 at 10:33

 

Toby Speight

23.4k4646 gold badges5757 silver badges8383 bronze badges

answered Apr 1 ’13 at 14:34

 

stdcall

23.6k1414 gold badges7272 silver badges118118 bronze badges

  • 119

You can also discard local changes for a specific file by doing: git checkout filename – ckb Apr 1 ’13 at 16:49

  • 14

By default git stash will not stash files for which there are no history. So if you have files which you have not yet added but which would be overwritten or “created” by the merge, then the merge will still block. In that situation, you can use git stash -u to stash uncommitted files too. Or you can just delete them! – joeytwiddle Mar 13 ’14 at 16:10

  • 28

running git clean -dfx was a terrible idea. Removed some .gitignored files I actually needed. – ezuk May 20 ’14 at 16:10

  • 5

I have encountered a situation where a user, after doing git reset –hard, still had unmerged changes! – Amedee Van Gasse May 11 ’15 at 11:45

  • 1

Just a note. I was having trouble with .gitignore not being in-sync with the origin. I had been using git update-index –assume-unchanged C:\…\.gitignore I had to change it to track changes again so I could use git stash as in: git update-index –no-assume-unchanged C:\…\.gitignore Then the above worked great! – mmv_sat Jul 23 ’15 at 15:04

Show 4 more comments

100

git stashgit pull <remote name> <remote branch name> (or) switch branchgit 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).

Share

Improve this answer

Follow

edited May 7 ’18 at 14:46

 

SOLO

76866 silver badges1818 bronze badges

answered Jun 4 ’15 at 6:58

 

Loganathan

1,44711 gold badge1212 silver badges1717 bronze badges

  • 1

stackoverflow.com/questions/15286075/…, can be useful as well – vikramvi Sep 18 ’18 at 15:47

  • 4

to explain @vikramvi’s point: we can also use git stash pop instead of git stash apply. The former removes it from stash while the latter still keeps it there – Anupam Feb 25 ’20 at 9:11

Add a comment

35

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 -fgit checkout master -f

reset

 

Discard the local changes

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

 

 

Leave a Reply

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