How do I force "git pull" to overwrite local files?

GitVersion ControlOverwriteGit PullGit Fetch

Git Problem Overview

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

The scenario is the 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.

This is the error I'm getting:

> 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 need to update on their computer.

Git Solutions

Solution 1 - Git

⚠ Important: If you have any local changes, they will be lost (if they're tracked). Also, with or without --hard option, any local commits that haven't been pushed will be lost.[*]

If you have any files that are not tracked by Git (e.g. uploaded user content), these files will not be affected.

First, run a fetch to update all origin/<branch> refs to latest:

git fetch --all

Backup your current branch:

git branch backup-master

Then, you have two options:

git reset --hard origin/master

OR If you are on some other branch:

git reset --hard origin/<branch_name>

git fetch downloads the latest from remote without trying to merge or rebase anything.

Then the git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master

Maintain current local commits

[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from master before resetting:

git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master

After this, all of the old commits will be kept in new-branch-to-save-current-commits.

Uncommitted changes

Uncommitted changes, however (even staged), will be lost. Make sure to stash and commit anything you need. For that you can run the following:

git stash

And then to reapply these uncommitted changes:

git stash pop

Solution 2 - Git

Try this:

git reset --hard HEAD
git pull

It should do what you want.

Solution 3 - Git

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:

# WARNING: this can't be undone!

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

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

Consider using -n (--dry-run) flag first. This will show you what will be deleted without actually deleting anything:

git clean -n -f -d

Example output:

Would remove untracked-file-1.txt
Would remove untracked-file-2.txt
Would remove untracked/folder

Solution 4 - Git

Like Hedgehog I think the answers are terrible. But though Hedgehog's answer might be better, I don't think it is as elegant as it could be. The way I found to do this is by using fetch and merge with a defined strategy. Which should make it so that your local changes are preserved as long as they are not one of the files that you are trying to force an overwrite with.

First do a commit of your changes
 git add *
 git commit -a -m "local file server commit message"
Then fetch the changes and overwrite if there is a conflict
 git fetch origin master
 git merge -s recursive -X theirs origin/master

-X is an option name, and theirs is the value for that option. You're choosing to use their changes (the other option is ours changes) if there is a conflict.

Solution 5 - Git

Instead of doing:

git fetch --all
git reset --hard origin/master

I'd advise doing the following:

git fetch origin master
git reset --hard origin/master

No need to fetch all remotes and branches if you're going to reset to the origin/master branch right?

Solution 6 - Git

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...

Solution 7 - Git

*Warning, doing this will permanently delete your files if you have any directory/ entries in your gitignore file.**

Some answers seem to be terrible. Terrible in the sense of what happened to @Lauri by following David Avsajanishvili suggestion.

Rather (git > v1.7.6):

git stash --include-untracked
git pull

Later you can clean the stash history.

Manually, one-by-one:

$ git stash list
stash@{0}: WIP on <branch>: ...
stash@{1}: WIP on <branch>: ...

$ git stash drop stash@{0}
$ git stash drop stash@{1}

Brutally, all-at-once:

$ git stash clear

Of course if you want to go back to what you stashed:

$ git stash list
$ git stash apply stash@{5}

Solution 8 - Git

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

Solution 9 - Git

Instead of merging with git pull, try this:

git fetch --all

followed by:

git reset --hard origin/master.

Solution 10 - Git

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.

Solution 11 - Git

The problem with all these solutions is that they are all either too complex or, an even bigger problem, is that they remove all untracked files from the webserver, which we don't want since there are always needed configuration files which are on the server and not in the Git repository.

Here is the cleanest solution which we are using:

# Fetch the newest code
git fetch

# Delete all files which are being added, so there
# are no conflicts with untracked files
for file in `git diff HEAD..origin/master --name-status | awk '/^A/ {print $2}'`
    rm -f -- "$file"

# Checkout all files which were locally modified
for file in `git diff --name-status | awk '/^[CDMRTUX]/ {print $2}'`
    git checkout -- "$file"

# Finally pull all the changes
# (you could merge as well e.g. 'merge origin/master')
git pull
  • The first command fetches the newest data.

  • The second command checks if there are any files that are being added to the repository and deletes those untracked files from the local repository which would cause conflicts.

  • The third command checks-out all the files which were locally modified.

  • Finally, we do a pull to update to the newest version, but this time without any conflicts, since untracked files which are in the repo don't exist anymore and all the locally modified files are already the same as in the repository.

Solution 12 - Git

First of all, try the standard way:

git reset HEAD --hard # To remove all not committed changes!
git clean -fd         # To remove all untracked (non-git) files and folders!

Warning: Above commands can results in data/files loss only if you don't have them committed! If you're not sure, make the backup first of your whole repository folder.

Then pull it again.

If above won't help and you don't care about your untracked files/directories (make the backup first just in case), try the following simple steps:

cd your_git_repo  # where 'your_git_repo' is your git repository folder
rm -rfv *         # WARNING: only run inside your git repository!
git pull          # pull the sources again

This will REMOVE all git files (excempt .git/ dir, where you have all commits) and pull it again.

Why git reset HEAD --hard could fail in some cases?

  1. Custom rules in .gitattributes file

Having eol=lf rule in .gitattributes could cause git to modify some file changes by converting CRLF line-endings into LF in some text files.

If that's the case, you've to commit these CRLF/LF changes (by reviewing them in git status), or try: git config core.autcrlf false to temporary ignore them.

  1. File system incompability

When you're using file-system which doesn't support permission attributes. In example you have two repositories, one on Linux/Mac (ext3/hfs+) and another one on FAT32/NTFS based file-system.

As you notice, there are two different kind of file systems, so the one which doesn't support Unix permissions basically can't reset file permissions on system which doesn't support that kind of permissions, so no matter how --hard you try, git always detect some "changes".

Solution 13 - Git

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

I solved it by:

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

Now it works.

Solution 14 - Git


In speaking of pull/fetch/merge in the previous answers, I would like to share an interesting and productive trick,

git pull --rebase

This above command is the most useful command in my Git life which saved a lot of time.

Before pushing your newly commit to server, try this command and it will automatically synchronise the latest server changes (with a fetch + merge) and will place your commit at the top in the Git log. There isn't any need to worry about manual pull/merge.

Find details in What does "git pull --rebase" do?.

Solution 15 - Git

Here is a generic solution if you do not always want to paste the branch name or you want to automate this within a script

git fetch
git reset --keep origin/$(git rev-parse --abbrev-ref HEAD)

If you want to reset your local changes too:

git fetch
git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)

You also could add a bash alias using this command:

alias gplf='git fetch && echo "HEAD was at $(git rev-parse --short HEAD)" && git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)'

Solution 16 - Git

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

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

Solution 17 - Git

I summarized other answers. You can execute git pull without errors:

git fetch --all
git reset --hard origin/master
git reset --hard HEAD
git clean -f -d
git pull

Warning: This script is very powerful, so you could lose your changes.

Solution 18 - Git

Based on my own similar experiences, the solution offered by Strahinja Kustudic above is by far the best. As others have pointed out, simply doing hard reset will remove all the untracked files which could include lots of things that you don't want removed, such as config files. What is safer, is to remove only the files that are about to be added, and for that matter, you'd likely also want to checkout any locally-modified files that are about to be updated.

That in mind, I updated Kustudic's script to do just that. I also fixed a typo (a missing ' in the original).


# Fetch the newest code
git fetch

# Delete all files which are being added,
# so there are no conflicts with untracked files
for file in `git diff HEAD..origin/master --name-status | awk '/^A/ {print $2}'`
	echo "Deleting untracked file $file..."
    rm -vf "$file"

# Checkout all files which have been locally modified
for file in `git diff HEAD..origin/master --name-status | awk '/^M/ {print $2}'`
	echo "Checking out modified file $file..."
    git checkout $file
# Finally merge all the changes (you could use merge here as well)
git pull

Solution 19 - Git

It seems like most answers here are focused on the master branch; however, there are times when I'm working on the same feature branch in two different places and I want a rebase in one to be reflected in the other without a lot of jumping through hoops.

Based on a combination of RNA's answer and torek's answer to a similar question, I've come up with this which works splendidly:

git fetch
git reset --hard @{u}

Run this from a branch and it'll only reset your local branch to the upstream version.

This can be nicely put into a git alias (git forcepull) as well:

git config alias.forcepull "!git fetch ; git reset --hard @{u}"

Or, in your .gitconfig file:

  forcepull = "!git fetch ; git reset --hard @{u}"


Solution 20 - Git

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.

Solution 21 - Git

I believe there are two possible causes of conflict, which must be solved separately, and as far as I can tell none of the above answers deals with both:

  • Local files that are untracked need to be deleted, either manually (safer) or as suggested in other answers, by git clean -f -d

  • Local commits that are not on the remote branch need to be deleted as well. IMO the easiest way to achieve this is with: git reset --hard origin/master (replace 'master' by whatever branch you are working on, and run a git fetch origin first)

Solution 22 - Git

An easier way would be to:

git checkout --theirs /path/to/file.extension
git pull origin master

This will override your local file with the file on git

Solution 23 - Git

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...

Solution 24 - Git

I have a strange situation that neither git clean or git reset works. I have to remove the conflicting file from git index by using the following script on every untracked file:

git rm [file]

Then I am able to pull just fine.

Solution 25 - Git

I know of a much easier and less painful method:

$ git branch -m [branch_to_force_pull] tmp
$ git fetch
$ git checkout [branch_to_force_pull]
$ git branch -D tmp

That's it!

Solution 26 - Git

Just do

git fetch origin branchname
git checkout -f origin/branchname // This will overwrite ONLY new included files
git checkout branchname
git merge origin/branchname

So you avoid all unwanted side effects, like deleting files or directories you wanted to keep, etc.

Solution 27 - Git

Despite the original question, the top answers can cause problems for people who have a similar problem, but don't want to lose their local files. For example, see Al-Punk and crizCraig's comments.

The following version commits your local changes to a temporary branch (tmp), checks out the original branch (which I'm assuming is master) and merges the updates. You could do this with stash, but I've found it's usually easier to simply use the branch / merge approach.

git checkout -b tmp
git add *; git commit -am "my temporary files"
git checkout master

git fetch origin master
git merge -s recursive -X theirs origin master

where we assume the other repository is origin master.

Solution 28 - Git


  1. Track local changes so no-one here ever loses them.
  2. Make the local repository match the remote origin repository.


  1. Stash the local changes.

  2. Fetch with a clean of files and directories ignoring .gitignore and hard reset to origin.

    git stash --include-untracked
    git fetch --all
    git clean -fdx
    git reset --hard origin/master

Solution 29 - Git

Reset the index and the head to origin/master, but do not reset the working tree:

git reset origin/master

Solution 30 - Git

These four commands work for me.

git reset --hard HEAD
git checkout origin/master
git branch -D master
git checkout -b master

To check/pull after executing these commands

git pull origin master

I tried a lot but finally got success with these commands.

Solution 31 - Git

I read through all the answers but I was looking for a single command to do this. Here is what I did. Added a git alias to .gitconfig

      fp = "!f(){ git fetch ${1} ${2} && git reset --hard ${1}/${2};};f"

Run your command as

git fp origin master

equivalent to

git fetch origin master
git reset --hard origin/master

Solution 32 - Git

Don't use git reset --hard. That will wipe their changes which may well be completely undesirable. Instead:

git pull
git reset origin/master
git checkout <file1> <file2> ...

You can of course use git fetch instead of git pull since it clearly isn't going to merge, but if you usually pull it makes sense to continue to pull here.

So what happens here is that git pull updates your origin/master reference; git reset updates your local branch reference on to be the same as origin/master without updating any files, so your checked-out state is unchanged; then git checkout reverts files to your local branch index state as needed. In cases where exactly the same file has been added on live and on upstream master, the index already matches the file following the reset, so in the common case you don't need to do git checkout at all.

If the upstream branch also contains commits which you want to apply automatically, you can follow a subtle variation on the process:

git pull
git merge <commit before problem commit>
git reset <problem commit>
git checkout <file1> <file2> ...
git pull

Solution 33 - Git

This is the best practice for reverting changes:

  • git commit Commit your staged changes so they will be saved in the reflog ( see below )
  • git fetch Fetch the latest upstream changes
  • git reset --hard origin/master Hard reset to the origin master branch

The reflog records branches and other references being updated in the local repository. Or simply put - the reflog is the history of your changes.

So it's always a great practice to commit. Commits are appended to the reflog which ensures you will always have a way to retrieve the deleted code.

Solution 34 - Git

I used this command to get rid of the local files preventing me from doing a pull/merge. But be careful! Run git merge … first to see whether there are only those files you really want to remove.

git merge origin/master 2>&1 >/dev/null | grep ^[[:space:]] | sed s/^[[:space:]]//g | xargs -L1 rm
  • git merge lists among other things all those files. They are prepended by some white-space.
  • 2>&1 >/dev/null redirects the error output to the standard one so it is picked up by grep.
  • grep ^[[:space:]] filters only the lines with file names.
  • sed s/^[[:space:]]//g trims the white-space from the beginning.
  • xargs -L1 rm calls rm on each of those files, deleting them.

Handle with care: Whatever git merge outputs, the rm will be called for every line beginning with a white-space.

Solution 35 - Git

I was trying to use the Material2 branch on the Angular2-Webpack-Starter and had a heck of a time. This was the only way I could download and use that branch.

git clone --depth 1

cd angular2-webpack-starter/

git checkout -b material2

Open the project folder and delete all non-hidden files and folders. Leave all the hidden ones.

git add .

git commit -m "pokemon go"

git reset --hard

git pull origin material2

(When the editor pops up, hit ':wq', and then press Enter)

Now you are ready.

Solution 36 - Git

On Windows, do this single command:

git fetch --all & git reset --hard origin/master

Solution 37 - Git

You could ignore that file with a file in your project base folder:



Then pull the changes and then remove that line from your gitignore file.

Solution 38 - Git

1: Reset to a previous commit

git reset --hard HEAD

2: Delete Untracked Files

git clean -f

3: Pull the commits

git pull


Solution 39 - Git

Another way of solving this is to first stash any uncommitted changes using git stash and then run

git pull --rebase=interactive -s recursive -X theirs

In the interactive rebase you can change all your local undesired commits to drop, which will get rid of them and leave you at the head of the remote branch without introducing a merge commit.

Now you can run git stash apply if you had local stashed changed that you want to bring back.

Solution 40 - Git

Despite the fact that this question has already many answers, the original question is to solve this question

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

As binary files can't be merged a simple answer is

git checkout public/images/icon.gif

With that the file will recover the previous state it had in this branch.

I usually do git stash if I don't want to lose my changes or something like git checkout . if I don't care about locally modified files. IMO much more simple than reset --hard, clean... and all this stuff more suited to leave the branch as in remote, including commits, untracked files, rather than just solving a locally modified file.

Solution 41 - Git

If you are working on your code and find the new changes a huge mistake or unwanted you can simply use an alternative like:

git restore .

where . means all the files present in the directory.

Solution 42 - Git

Once you do git pull, you will get list of files that are not matching. If the number of files is not very large then you can checkout those files, this action will overwrite those files.

git checkout -- <filename>

I usually do it if for quick check I modify local files on server (no recommended and probabaly reason behind you getting this issue :D ), I checkout the changed files after finding solution.

Solution 43 - Git

I did this to get it to work:

On the computer where I created the new branch:

git push --all

On the computer where I wanted the new branch to show:

git fetch --all

Solution 44 - Git

As I often need a fast way to reset the current branch on Windows through the command prompt, here's a fast way:

for /f "tokens=1* delims= " %a in ('git branch^|findstr /b "*"') do @git reset --hard origin/%b

Solution 45 - Git

Step 1. (optional)
From the root of your local repository, save a backup and empty the current folder:

mkdir -p ../<branch>-bkp && mv --backup=t * ../<branch>-bkp

Step 2. Download all files and folders of the branch from the remote repository:

git checkout <branch> && git add -A . && git reset --hard origin/<branch> && git pull

where you should replace <branch> with the name of the branch you want to overwrite.


  • You may prefer to manually backup and remove the current files and folders instead of step 1.
    In fact, I recommend doing this with a file-handler in the GUI of your operating system.
  • If you skip step 1, beware: you will lose all work in the local repository!
  • Important: if you leave out git pull in step 2,
    you risk not getting the latest version from the remote repository!
    According to the second reference below, git reset --hard will
    reset the staging area and the working directory to match the most recent commit.
    My experience contradicts this claim!
  • If you run git reset --hard origin/<branch_to_overwrite> without first deleting all files and folders from your local repository, beware that any junk files still laying around may sneak into the remote repository at a later git push even if that was not your intention.
    The extra git add -A . in step 2 prevents this from happening if you choose to leave out step 1.


Solution 46 - Git

I’ve tried most of what I could find here, none worked in my case.

What worked was git merge -X theirs

Solution 47 - Git

You could try git pull --force or maybe stash your commits by using git stash and then running git pull.

Solution 48 - Git

I think you can also force by git push -f / git push --force


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakub TroszokView Question on Stackoverflow
Solution 1 - GitRNAView Answer on Stackoverflow
Solution 2 - GitTravis ReederView Answer on Stackoverflow
Solution 3 - GitDavid AvsajanishviliView Answer on Stackoverflow
Solution 4 - GitRichard KerseyView Answer on Stackoverflow
Solution 5 - GitJohannekeView Answer on Stackoverflow
Solution 6 - GitJakub TroszokView Answer on Stackoverflow
Solution 7 - GitHedgehogView Answer on Stackoverflow
Solution 8 - GitVishalView Answer on Stackoverflow
Solution 9 - GitLloyd MooreView Answer on Stackoverflow
Solution 10 - GitChris BIllanteView Answer on Stackoverflow
Solution 11 - GitStrahinja KustudicView Answer on Stackoverflow
Solution 12 - GitkenorbView Answer on Stackoverflow
Solution 13 - GitJohn John PichlerView Answer on Stackoverflow
Solution 14 - GitSazzad Hissain KhanView Answer on Stackoverflow
Solution 15 - GitwarchView Answer on Stackoverflow
Solution 16 - GitRyanView Answer on Stackoverflow
Solution 17 - GitRobert MoonView Answer on Stackoverflow
Solution 18 - GitRolf KaiserView Answer on Stackoverflow
Solution 19 - GitJacobEvelynView Answer on Stackoverflow
Solution 20 - GitTierliebView Answer on Stackoverflow
Solution 21 - GittihoView Answer on Stackoverflow
Solution 22 - Gitmaximus 69View Answer on Stackoverflow
Solution 23 - GitSimon B.View Answer on Stackoverflow
Solution 24 - GitChen ZhangView Answer on Stackoverflow
Solution 25 - GitddmytrenkoView Answer on Stackoverflow
Solution 26 - Gituser2696128View Answer on Stackoverflow
Solution 27 - GitSnowcrashView Answer on Stackoverflow
Solution 28 - GitvezenkovView Answer on Stackoverflow
Solution 29 - Gituser811773View Answer on Stackoverflow
Solution 30 - GitVishesh ChandraView Answer on Stackoverflow
Solution 31 - GitVenkat KotraView Answer on Stackoverflow
Solution 32 - GitJim DriscollView Answer on Stackoverflow
Solution 33 - GitJordan GeorgievView Answer on Stackoverflow
Solution 34 - GitGlutexoView Answer on Stackoverflow
Solution 35 - GitPost ImpaticaView Answer on Stackoverflow
Solution 36 - GitLuca C.View Answer on Stackoverflow
Solution 37 - GitDanielGView Answer on Stackoverflow
Solution 38 - GitabhijithvijayanView Answer on Stackoverflow
Solution 39 - GitjoelostblomView Answer on Stackoverflow
Solution 40 - GitguillemView Answer on Stackoverflow
Solution 41 - GitNisarg JhatakiaView Answer on Stackoverflow
Solution 42 - GitAshutosh NigamView Answer on Stackoverflow
Solution 43 - GitdaddeView Answer on Stackoverflow
Solution 44 - GitnpocmakaView Answer on Stackoverflow
Solution 45 - GitHenkeView Answer on Stackoverflow
Solution 46 - GitSxilderikView Answer on Stackoverflow
Solution 47 - GitUltan KearnsView Answer on Stackoverflow
Solution 48 - GitbombomView Answer on Stackoverflow