git force pull

How to Force Git Pull to Overwrite Local Files

Git pull is the command used to download files & folders from remote repository and update the local repository to match the contents of remote repository. But when you use git pull, sometimes you may get an error saying ‘untracked working tree file will be overwritten by merge’. For example, if you have a local file with same filename as that on the server, you will get this error. In such cases, you may want to overwrite local files during git pull.

How to Force Git Pull to Overwrite Local Files

Here are the steps to force git pull to overwrite local files. Please note, if you have any local changes that are tracked they will be overwritten. Same is true for any local commits that have not been pushed to remote repository.

1. Stash Uncommitted Changes

First, we need to stash uncommitted changes so that they are not lost due to git pull.

$ git stash

You can always add back the stashed changes using the following command.

$ git stash pop

2. Backup Branch

Next, we will backup the master branch which will be overwritten after git pull. You can do so using the following command.

$ git branch backup-master

If you only want to backup a specific branch, use the following commands. In this case, we first checkout the required branch, and then create a new branch backup-branch out of it.

$ git checkout branch_name
$ git branch backup-branch

3. Git Fetch

We will run git fetch command to download the latest version of your remote repository to your local branch. The difference between git fetch and git pull is that git fetch will only download the latest version of your remote repository but not merge it with your local repository, but git pull will both download as well as merge the remote repository. Run the following command to fetch all branches from your repository.

$ git fetch --all

If you want to download only specific branch then run the following command. Replace <branch_name> with your branch name.

git fetch origin/<branch_name>

4. Git Reset

Finally, you can run git reset command to merge the fetched content with your local repo and overwrite the changes.

$ git reset --hard origin/master

If you want to merge only a specific branch from remote repository to your local repository, run the following command instead.

$ git reset --hard origin/<branch_name>

In this article, we have learnt how to overwrite local files and folders when you use git pull. We have used a combination of git fetch and git reset, instead of directly using git pull so that we have more control over the process. Using git pull will directly download and merge remote content with local content and might end up giving too many errors, which are tedious to debug. In this case, we go step by step so that if things don’t work out as expected, we can easily revert them.

Also read:

How to Delete Git Branch Locally & Remotely
How to Reset or Revert File to Specific Commit in Git
How to Find & Restore Deleted File in Git Repository
How to Set Vim Default Editor for Git
How to Use Variable as Key in JS Object

Leave a Reply

Your email address will not be published.