1064

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?

Tobi
  • 2,591
  • 15
  • 34
Jo Sprague
  • 16,523
  • 10
  • 42
  • 62
  • Possible duplicate of [How to ignore error on git pull about my local changes would be overwritten by merge?](https://stackoverflow.com/questions/14318234/how-to-ignore-error-on-git-pull-about-my-local-changes-would-be-overwritten-by-m) – hestellezg Jul 14 '17 at 18:48
  • 23
    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

23 Answers23

1804

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

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
stdcall
  • 27,613
  • 18
  • 81
  • 125
  • @Mellowcandle tried it. Not working for me. git checkout is also not working! Confused!! I'm doing a full clone to another directory and manually putting in non-indexed files one by one. This freaks me! – retromuz Jun 25 '13 at 22:39
  • 21
    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
  • 32
    running `git clean -dfx` was a terrible idea. Removed some .gitignored files I actually needed. – ezuk May 20 '14 at 16:10
  • 11
    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
  • 4
    Discard local changes for a specific file using `git checkout filename` awesome point – anjaneyulubatta505 Apr 18 '18 at 11:14
  • 1
    But what if I want to keep the changes? And not have my changes overwritten? – Philip Rego Jul 09 '19 at 20:23
  • but why in egit (eclipse git plugin), I can merge with origin/master even my local branch has uncommitted change? – user1169587 Jun 04 '20 at 10:03
  • 1
    I find this rather confusing. I came across this page looking for instructions to MERGE my local changes with the remote changes, which seems to be actually the whole reason anyone even needs git. Otherwise, we could just email our changes back and forth. I used your "git stash" "solution" and I'm worse off: CONFLICT: Merge conflict... I have "Unmerged paths" and two options - to unstage or to mark resolution. I've solved nothing. – Chuckk Hubbard Aug 15 '22 at 09:44
123
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).

SOLO
  • 868
  • 9
  • 19
Loganathan
  • 1,697
  • 1
  • 13
  • 17
  • 1
    https://stackoverflow.com/questions/15286075/difference-between-git-stash-pop-and-git-stash-apply, can be useful as well – vikramvi Sep 18 '18 at 15:47
  • 8
    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 09:11
55

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?

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • 2
    Dosn't work: I get still the same message like "stash your changes first". When I type "git stash" and then "git pull" -> "error: you have unsaved changes.. do a stash first". Short before destroying my computer – trinity420 Jan 21 '18 at 14:19
  • @trinity420 Could it be your file permissions, check `git status` what changes do you have after stashing. If none answer helps, consider adding a new question. – kenorb Jan 21 '18 at 14:40
  • thank you but my problem solved, tried everything on here, nothing worked, then clicked "commit changes" "merge" in PHPStorm and then I unstashed changes and it worked.. – trinity420 Jan 21 '18 at 15:52
28

use :

git reset --hard

then :

git pull origin master

Gata
  • 361
  • 3
  • 4
25

Try this

git stash save ""

and try pull again

Mr Nobody
  • 397
  • 4
  • 4
  • 3
    In my case I need to use `git stash -u`, as commented at https://stackoverflow.com/questions/15745045/how-do-i-resolve-git-saying-commit-your-changes-or-stash-them-before-you-can-me#comment34028978_15745424 – 林果皞 May 19 '20 at 11:36
17

This solved my error:

I am on branch : "A"

git stash

Move to master branch:

git checkout master 
git pull*

Move back to my branch: "A"

git checkout A 
git stash pop*
13

So the situation that I ran into was the following:

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.

except, right before that, was remote: so actually this:

remote: error: Your local changes to the following files would be overwritten by merge: some/file.ext Please, commit your changes or stash them before you can merge.

What was happening was (I think, not 100% positive) the git post receive hook was starting to run and screwing up due to movement changes in the remote server repository, which in theory, shouldn't have been touched.

So what I ended up doing by tracing through the post-receive hook and finding this, was having to go to the remote repository on the server, and there was the change (which wasn't on my local repository, which, in fact, said that it matched, no changes, nothing to commit, up to date, etc.) So while on the local, there were no changes, on the server, I then did a git checkout -- some/file.ext and then the local and remote repositories actually matched and I could continue to work, and deploy. Not entirely sure how this situation occurred, though a couple dozen developers plus IT changes may had something to do with it.

Mike
  • 1,199
  • 1
  • 15
  • 24
  • 2
    Is it a question or an answer? – stdcall Nov 29 '13 at 16:54
  • 2
    @stdcall - A bit of both. When I ran into this situation as described in the question, this is what I had to do to fix it. It was definitely not a normal git resolving, and from the question, it seems as though it could be the same abnormal situation (i.e., config changes on the server, but the local has no changes). If anyone has more of an idea as to why (or how) this happened, I would welcome any insight. – Mike Dec 02 '13 at 18:27
13

WARNING: This will delete untracked files, so it's not a great answer to this question.

In my case, I didn't want to keep the files, so this worked for me:

Git 2.11 and newer:

git clean  -d  -fx .

Older Git:

git clean  -d  -fx ""

Reference: http://www.kernel.org/pub/software/scm/git/docs/git-clean.html

  • -x means ignored files are also removed as well as files unknown to git.

  • -d means remove untracked directories in addition to untracked files.

  • -f is required to force it to run.

Indranil
  • 2,229
  • 2
  • 27
  • 40
13

To keep record of your newly created files while resolving this issue:

If you have newly created files, you can create a patch of local changes, pull in remote merges and apply your local patch after the remote merge is complete as defined step by step below:

  1. Stage your local changes. (do not commit). Staging is required to create patch of new created files (as they are still untracked)

git add .

  1. Create a patch to keep record

git diff --cached > mypatch.patch

  1. Discard local changes and delete new local files

git reset --hard

  1. Pull changes

git pull

  1. Apply your patch

git apply mypatch.patch

Git will merge changes and create .rej files for changes which are not merged.

As suggested by Anu, if you have issues applying patch, try:

git apply --reject --whitespace=fix mypatch.patch This answer git: patch does not apply talks in detail about this issue

Enjoy your continued work on your feature, and commit your local changes when done.

Community
  • 1
  • 1
Manpreet
  • 2,450
  • 16
  • 21
  • I wanted to push a portion of code with new changes so I did : 1. created a patch out of my local dev branch 2. did the hard reset 3. pull the new changes from master to dev(to avoid any merge conflicts) 4. did a small change in my local dev 5. pushed to remote dev 6. applied patch back--> Got error: `error: patch failed: yourfile.py:33 error: yourfile.py: patch does not apply` , I still have the mypatch.patch, but don't know why it's not getting applied and I lost my changes! – Anu Feb 12 '20 at 00:28
  • I got it, the correct command was `git apply --reject --whitespace=fix mypatch.patch`, I got my changes back phew!!! [Thanks to ](https://stackoverflow.com/a/15375869/6484358) – Anu Feb 12 '20 at 00:31
  • 1
    Anu, the command git apply mypatch.patch is correct to apply patch, this is what I use all the time, there might be some issue with the created patch itself, and you never lose your changes if you have your patch in hand, it contains all the consolidated changes. – Manpreet Feb 12 '20 at 04:34
  • "git apply --reject --whitespace=fix" mypatch.patch This command will apply the patch not resolving it leaving bad files as *.rej: patch -p1 < mypatch.patch – xpredo May 09 '21 at 04:59
7

For me this worked:

git reset --hard

and then

git pull origin <*current branch>

after that

git checkout <*branch>

Abhishek Lodha
  • 737
  • 2
  • 7
  • 30
Yeshi
  • 281
  • 3
  • 5
6

Discard the local changes using git reset --hard

Sanaullah Ahmad
  • 474
  • 4
  • 12
5

Asking for commit before pull

  • git stash
  • git pull origin << branchname >>

If needed :

  • git stash apply
Rahul Mankar
  • 910
  • 9
  • 17
  • 1
    Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit. – Pushpak Sharma Nov 01 '18 at 06:33
5

For me, only git reset --hard worked.

Commiting was not an option, as there was nothing to commit.

Stashing wasn't an option because there was nothing to stash.

Looks like it could have been from excluded files in .git/info/exclude and having git update-index --assume-unchanged <file>'ed some files.

Leo
  • 10,407
  • 3
  • 45
  • 62
1

Before using reset think about using revert so you can always go back.

https://www.pixelstech.net/article/1549115148-git-reset-vs-git-revert

On request

Source: https://www.pixelstech.net/article/1549115148-git-reset-vs-git-revert

git reset vs git revert   sonic0002        2019-02-02 08:26:39 

When maintaining code using version control systems such as git, it is unavoidable that we need to rollback some wrong commits either due to bugs or temp code revert. In this case, rookie developers would be very nervous because they may get lost on what they should do to rollback their changes without affecting others, but to veteran developers, this is their routine work and they can show you different ways of doing that. In this post, we will introduce two major ones used frequently by developers.

  • git reset
  • git revert

What are their differences and corresponding use cases? We will discuss them in detail below. git reset Assuming we have below few commits. enter image description here

Commit A and B are working commits, but commit C and D are bad commits. Now we want to rollback to commit B and drop commit C and D. Currently HEAD is pointing to commit D 5lk4er, we just need to point HEAD to commit B a0fvf8 to achieve what we want.  It's easy to use git reset command.

git reset --hard a0fvf8

After executing above command, the HEAD will point to commit B. enter image description here

But now the remote origin still has HEAD point to commit D, if we directly use git push to push the changes, it will not update the remote repo, we need to add a -f option to force pushing the changes.

git push -f

The drawback of this method is that all the commits after HEAD will be gone once the reset is done. In case one day we found that some of the commits ate good ones and want to keep them, it is too late. Because of this, many companies forbid to use this method to rollback changes.

git revert The use of git revert is to create a new commit which reverts a previous commit. The HEAD will point to the new reverting commit.  For the example of git reset above, what we need to do is just reverting commit D and then reverting commit C. 

git revert 5lk4er
git revert 76sdeb

Now it creates two new commit D' and C',  enter image description here

In above example, we have only two commits to revert, so we can revert one by one. But what if there are lots of commits to revert? We can revert a range indeed.

git revert OLDER_COMMIT^..NEWER_COMMIT

This method would not have the disadvantage of git reset, it would point HEAD to newly created reverting commit and it is ok to directly push the changes to remote without using the -f option. Now let's take a look at a more difficult example. Assuming we have three commits but the bad commit is the second commit.  enter image description here

It's not a good idea to use git reset to rollback the commit B since we need to keep commit C as it is a good commit. Now we can revert commit C and B and then use cherry-pick to commit C again.  enter image description here

From above explanation, we can find out that the biggest difference between git reset and git revert is that git reset will reset the state of the branch to a previous state by dropping all the changes post the desired commit while git revert will reset to a previous state by creating new reverting commits and keep the original commits. It's recommended to use git revert instead of git reset in enterprise environment.  Reference: https://kknews.cc/news/4najez2.html

Jan
  • 35
  • 4
  • Thanks for the answer. It adds new information, however, explaining it in more detail here in the answer could be more helpful in case the link breaks in the future. You can use quotes as well to quote excerpts from the website. – ranka47 Feb 12 '21 at 16:31
1

% git status HEAD detached at 5c Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory)

Wolf
  • 53
  • 1
  • 5
1

If only one of file is updated locally and you want to discard changes from that file then you can do following --

// Pull latest activities-page.tsx from remote and discard local changes.
git checkout activities-page.tsx  
//Pull latest branch
git pull origin master
Vikas Piprade
  • 272
  • 2
  • 7
0

In my case, I backed up and then deleted the file that Git was complaining about, committed, then I was able to finally check out another branch.

I then replaced the file, copied back in the contents and continued as though nothing happened.

CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
0

This is probably being caused by CRLF issues.

See: Why should I use core.autocrlf=true in Git?

Use this to pull and force update:

git pull origin master
git checkout origin/master -f
Keith Turkowski
  • 751
  • 7
  • 11
0

I tried the first answer: git stash with the highest score but the error message still popped up, and then I found this article to commit the changes instead of stash 'Reluctant Commit'

and the error message disappeared finally:

1: git add .

2: git commit -m "this is an additional commit"

3: git checkout the-other-file-name

then it worked. hope this answer helps.:)

Sophie cai
  • 195
  • 4
  • 13
0

If you are using Git Extensions you should be able to find your local changes in the Working directory as shown below:

enter image description here

If you don't see any changes, it's probably because you are on a wrong sub-module. So check all the items with a submarine icon as shown below:

enter image description here

When you found some uncommitted change:

Select the line with Working directory, navigate to Diff tab, Right click on rows with a pencil (or + or -) icon, choose Reset to first commit or commit or stash or whatever you want to do with it.

Bizhan
  • 16,157
  • 9
  • 63
  • 101
0

Probably

git --rebase --autostash

would help

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
0

I faced a similar issue with .env file on my NestJS project.

Tried many approaches including the ones on this page.

Finally, copied the contents of .env on clipboard, deleted the file and git pull worked!

Recreated .env and pasted contents back in.

In case of multiple files just move them temporarily outside of git repo directory and move them back after pulling your changes.

0x550x42
  • 27
  • 1
  • 6
-5

If you have made changes in any of your files do;

git add .

then;

git commit -m "your message"

After this you can then fetch from or push to your repo successfully.