Git pull - Please move or remove them before you can merge

205,869

Solution 1

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

Solution 2

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

git clean -d -f

then try git pull origin master

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

Solution 3

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

Solution 4

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
Share:
205,869

Related videos on Youtube

Brett
Author by

Brett

Updated on April 04, 2020

Comments

  • Brett
    Brett about 4 years

    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?

  • Brett
    Brett about 8 years
    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.
  • Zbynek Vyskovsky - kvr000
    Zbynek Vyskovsky - kvr000 about 8 years
    @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.
  • Brett
    Brett about 8 years
    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.
  • Zbynek Vyskovsky - kvr000
    Zbynek Vyskovsky - kvr000 about 8 years
    @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.
  • Brett
    Brett about 8 years
    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.
  • Zbynek Vyskovsky - kvr000
    Zbynek Vyskovsky - kvr000 about 8 years
    @Brett : Double check that these files are really not in the repo. In such case git wouldn't really complain, it complains only about the files which do cause the conflict, i.e. exist in both remote repository and being untracked on local file system.
  • Brett
    Brett about 8 years
    Ok, I found some that are still in the repo that are in .gitignore. I will clean that up and see how I go.
  • Stepan Yakovenko
    Stepan Yakovenko over 5 years
    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.
  • Steve
    Steve about 5 years
    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
  • Zappy.Mans
    Zappy.Mans over 3 years
    it is work . This solution should be accepted as answer.
  • Atif Shafi
    Atif Shafi over 2 years
    what if i wanted to force merge incoming files in my local repo
  • kayhanozturk
    kayhanozturk over 2 years
    thanks, it's working for me. I've used before reverting commit.