How do I force “git pull” to overwrite local files?

0 votes
asked Jul 14, 2009 by jakub-troszok

How do I force an overwrite of local files on a git pull?

The scenario is following:

  • A team member is modifying the templates for a website we are working on
  • They are adding some images to the images directory (but forgets to add them under source control)
  • They are sending the images by mail, later, to me
  • I'm adding the images under the source control and pushing them to GitHub together with other changes
  • They cannot pull updates from GitHub because Git doesn't want to overwrite their files.

The errors I'm getting are:

error: Untracked working tree file 'public/images/icon.gif' would be overwritten by merge.

How do I force Git to overwrite them? The person is a designer - usually I resolve all the conflicts by hand, so the server has the most recent version that they just needs to update on their computer.

29 Answers

0 votes
answered Jul 14, 2009 by jakub-troszok

It looks like the best way is to first do:

git clean

To delete all untracked files and then continue with the usual git pull...

0 votes
answered May 9, 2010 by travis-r

Try this:

git reset --hard HEAD
git pull

It should do what you want.

0 votes
answered Aug 5, 2010 by vishal

You might find this command helpful to throw away local changes:

git checkout <your-branch> -f

And then do a cleanup (removes untracked files from the working tree):

git clean -f

If you want to remove untracked directories in addition to untracked files:

git clean -fd
0 votes
answered Dec 3, 2010 by simon-b

I just solved this myself by:

git checkout -b tmp # "tmp" or pick a better name for your local changes branch
git add -A
git commit -m 'tmp'
git pull
git checkout master # Or whatever branch you were on originally
git pull
git diff tmp

where the last command gives a list of what your local changes were. Keep modifying the "tmp" branch until it is acceptable and then merge back onto master with:

git checkout master && git merge tmp

For next time, you can probably handle this in a cleaner way by looking up "git stash branch" though stash is likely to cause you trouble on the first few tries, so do first experiment on a non-critical project...

0 votes
answered Jan 12, 2011 by john-john-pichler

I had the same problem. No one gave me this solution, but it worked for me.

I solved it by:

  1. Deleting all the files. Leave just the .git directory.
  2. git reset --hard HEAD
  3. git pull
  4. git push

Now it works.

0 votes
answered Jan 14, 2011 by ryan

I had a similar problem. I had to do this:

git reset --hard HEAD
git clean -f
git pull
0 votes
answered Mar 19, 2011 by david-avsajanishvili

WARNING: git clean deletes all your untracked files/directories and can't be undone.

Sometimes just clean -f does not help. In case you have untracked DIRECTORIES, -d option also needed:

git reset --hard HEAD
git clean -f -d
git pull

WARNING: git clean deletes all your untracked files/directories and can't be undone.

0 votes
answered May 5, 2011 by chris-billante

The only thing that worked for me was:

git reset --hard HEAD~5

This will take you back five commits and then with

git pull

I found that by looking up how to undo a Git merge.

0 votes
answered Aug 3, 2011 by tierlieb

I had the same problem and for some reason, even a git clean -f -d would not do it. Here is why: For some reason, if your file is ignored by Git (via a .gitignore entry, I assume), it still bothers about overwriting this with a later pull, but a clean will not remove it, unless you add -x.

0 votes
answered Sep 19, 2011 by chen-zhang

I have a strange situation that either git clean or git reset works. I have to remove the conflicting file from git index by

git rm [file]

Then I can pull just fine.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.