Skip to main content

How do I force git pull to overwrite local files?

Short answer

There is no single git pull --force command; to forcibly overwrite your local branch with the remote state, you use a two-step approach: first fetch, then hard-reset. Run git fetch origin to download the latest remote state, then git reset --hard origin/<branch-name> to move your local branch pointer to exactly match the remote, discarding all local commits and staged changes. If you also have untracked files you want to remove, follow up with git clean -fd to delete them — note this is permanent and cannot be undone. Before executing these destructive commands, run git status and git stash push to preserve any work in progress you might still need. This workflow is typically used to recover from a botched local history or to sync after a collaborator has force-pushed to the remote branch with your knowledge.

When working on a project with a team, you might stumble upon error messages like these when trying to pull from a remote repository:

error: Your local changes to the following files would be overwritten by merge: ...

-or-

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

-or-

Please clean your repository working tree before checkout.

The reason for error messages like these is rather simple: you have local changes that would be overwritten by the incoming new changes that a "git pull" would bring in. The last error often appears in VS Code or when switching branches with uncommitted changes.

No, git pull does not overwrite local changes. Git will abort the pull and show an error if your uncommitted changes conflict with incoming changes. This is a safety mechanism — your local work remains untouched until you explicitly decide what to do with it.

For this reason, there is no "force pull" feature in Git — but you can perform a couple of steps to achieve the same result: force pulling from the remote and overwriting your local files.

What about git pull --force?

You might expect that git pull --force would force Git to overwrite your local changes. It does not. The --force flag is passed down to git fetch, not git merge. It allows Git to update your local tracking references even when the remote history has been rewritten (e.g., after someone ran git push --force on the remote).

In other words, git pull --force:

  • does NOT overwrite uncommitted changes
  • does NOT overwrite local commits
  • does NOT discard your working directory changes

It simply allows the fetch step to handle non-fast-forward ref updates. To actually overwrite local files, follow the steps below.

Step 1: Cleaning Up the Working Copy

First, you'll need to make sure your working copy doesn't contain these conflicting changes anymore. There are two ways to achieve this:

a) Saving Local Changes on a Stash

If you want to preserve your local changes, you can safely store them on a Stash. They will be available in case you want them back at a later point.

git stash --include-untracked

b) Discarding Local Changes

If you are sure that you don't need them anymore, you can discard your local changes completely:

Careful: this command can discard work.

git reset --hard

If you also have untracked / new files, you will have to use the "git clean" command to get rid of these, too:

Careful: this command can discard work.

git clean -fd

Please be careful with these commands: discarding local changes and untracked files cannot be undone!

Step 2: Pull Again

After you have cleaned up any local changes / untracked files that would have been overwritten, the pull will finally work:

git pull

Alternative: Using fetch + reset

Another common approach is to use git fetch combined with git reset --hard. This downloads the latest remote state and resets your local branch to match it exactly:

Careful: this command can discard work.

git fetch origin
git reset --hard origin/main

Unlike the stash + pull approach, this also discards any local commits that haven't been pushed yet. If you want to keep those commits around as a backup, create a branch first:

Careful: this command can discard work.

git branch backup-branch
git fetch origin
git reset --hard origin/main

You can also use the @{u} shorthand to refer to the upstream branch, regardless of its name:

Careful: this command can discard work.

git fetch origin
git reset --hard @{u}

If you also have untracked files that need to be removed:

Careful: this command can discard work.

git fetch origin
git reset --hard origin/main
git clean -fd

What About git checkout --force and Untracked Files?

If your error is related to switching branches rather than pulling, you might be using git checkout -f or git switch --force. Here's what you should know:

Command Tracked files (modified) Untracked files
git checkout -f Changes are discarded NOT removed — switch is blocked if they conflict
git switch --force Changes are discarded NOT removed — switch is blocked if they conflict
git clean -fd Not affected Removed

To force a branch switch when untracked files are in the way:

Careful: this command can discard work.

git clean -fd
git checkout -f main

Or with git switch:

Careful: this command can discard work.

git clean -fd
git switch --force main

Auto-Stashing in Tower

If you're using the Tower Git client, you’ll notice that it helps you avoid these situations: whenever you have uncommitted local changes present and want to perform an action like Pull, Checkout or Merge, Tower will automatically offer to store these changes safely on a Stash.

This way, you neither have to take any extra steps nor do you have to think about this anymore.


Learn More