101

I am trying to do a git pull origin master from my server but keep getting the error:

Please move or remove them before you can merge.

There are no untracked files, but it seems like it has issues with the ignored files for some reason.

I tried running a git clean -nd to see what would be deleted and it lists a whole bunch of files that are ignored in .gitignore.

How can I fix this so I can do a pull?

1

7 Answers 7

279

I just faced the same issue and solved it using the following. First clear tracked files by using :

# caution: it will **delete all untracked files**
git clean -d -f

then try git pull origin master

You can view other git clean options by typing git clean -help

5
  • 7
    Doesn't work, I still get: error: The following untracked working tree files would be removed by merge: logs/recommend.log Please move or remove them before you can merge. Commented Dec 7, 2018 at 11:30
  • 3
    it is work . This solution should be accepted as answer.
    – Zappy.Mans
    Commented Aug 15, 2020 at 2:10
  • what if i wanted to force merge incoming files in my local repo
    – Atif Shafi
    Commented Nov 15, 2021 at 3:26
  • 1
    thanks, it's working for me. I've used before reverting commit. Commented Jan 14, 2022 at 8:50
  • 1
    This answer needs to be preceded by a CAUTION. I performed this command and git deleted a lot of untracked useful files under my directory, and it was unrecoverable.
    – M.Zhu
    Commented Aug 3, 2023 at 7:56
38

To remove & delete all changes git clean -d -f

1
  • 3
    Thanks. That worked for me. One thing to watch for: it deletes all untracked files so if you have a .env file or other local config with secrets in it, make a backup first
    – Steve
    Commented Feb 26, 2019 at 20:56
33

Apparently the files were added in remote repository, no matter what was the content of .gitignore file in the origin.

As the files exist in the remote repository, git has to pull them to your local work tree as well and therefore complains that the files already exist.

.gitignore is used only for scanning for the newly added files, it doesn't have anything to do with the files which were already added.

So the solution is to remove the files in your work tree and pull the latest version. Or the long-term solution is to remove the files from the repository if they were added by mistake.

A simple example to remove files from the remote branch is to

$git checkout <brachWithFiles>
$git rm -r *.extension
$git commit -m "fixin...."
$git push

Then you can try the $git merge again

7
  • I just added the repo to the server itself, so I went an extra step and removed it completely... then I did a git add --all on the fresh repo so it now should not be adding any ignored files. Then I committed and then did a git pull origin master but same problem still exists.
    – Brett
    Commented Mar 16, 2016 at 15:57
  • @Brett : the issue seems to exist in remote repo, not the local one. Remove the local and then pull. After that remove the files which caused the issue, commit and push. Since then the files should be ignored. Commented Mar 16, 2016 at 16:47
  • What do you mean by local? The repo on my server? The repo is in three places, local (my dev machine), bitbucket (remote) and the server - the issue I am experiencing is on the server. I don't want to psychically remove these files, just ignore them - they exist on the server, but nowhere else.
    – Brett
    Commented Mar 16, 2016 at 17:00
  • @Brett : I mean the one from which you pull the update, I.e. the bitbucket. I assume you'll do the above on your local dev repo and push the changes to bitbucket. Then you'll simply run pull on server and as the files will be removed from bitbucket repo, the issues will be gone. Commented Mar 16, 2016 at 19:03
  • Thing is, the files it is having problems with are ignored files that exist on the server only - the server has some folders that don't exist locally and hence I added them to .gitignore; so I'm not understanding why Git can't just ignore them - they aren't in the repo and are ignored.
    – Brett
    Commented Mar 16, 2016 at 19:30
6

I got this error due to the case-sensitive names of files. What I did is change the configuration of git to ignore case-sensitive by running

git config core.ignorecase true  

then tried again to pull again and it worked.

git pull

You may return the ignorecase back to false

git config core.ignorecase false   
5

If there are too many files to delete, which is actually a case for me. You can also try the following solution:

1) fetch

2) merge with a strategy. For instance this one works for me:

git.exe merge --strategy=ours master
1
git reset --hard

It actually reset all changes on your local server.

0

If you are getting error like

  • branch master -> FETCH_HEAD error: The following untracked working tree files would be overwritten by merge: src/dj/abc.html Please move or remove them before you merge. Aborting

Try removing the above file manually(Careful). Git will merge this file from master branch.

Not the answer you're looking for? Browse other questions tagged or ask your own question.