How to force git pull to overwrite local files ?

How to force git pull to overwrite local files ?

📋 Table Of Content

This article is about how to force git pull to overwrite local files.

Sometimes when we do a pull request on a particular branch we get error like:

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

These error occurs because we have local changes that will overwritten when we will do a pull request on the same branch.

For safety reason, git wont allow to overwrite the changes or do a force pull over it.

However, we can follow certain steps which will help accomplished our goal of doing a force pull.

Force git pull on local files

Follow the steps below to do a force git pull to overwrite local changes.

Step 1 : Run a fetch to update all branches in your repository. It will get the latest files from the remote repository without merging them.

git fetch --all

Step 2 : Now we have to clean or stash up our file changes. We can do it in two ways:

  • Saving local changes on stash : It will store all the uncommitted changes we made locally on a stash.
git stash -u

// OR

git stash --all
  • Discarding local change: By discarding it, you are deleting all the changes you made. To do that we will reset our branch to the previous pulled state.
git reset --hard origin/master


git reset --hard origin/<branch_name>

And if you have any untracked files, you just have to clean those files the local branch.

git clean -fd


git clean -f -d

Step 3 : Now as you have clean all the untracked files / new files. you just have to a git pull again.

git pull origin master

That's it now you can pull your changes again and code on your project. 🧑🏻‍💻

Related Topics:

How To Create New Folder In Github?

Resolve- Refusing To Merge Unrelated Histories Git Error

Open Github Repository Directly In Vscode In Browser Online