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

Created by RNALinked to 61.2m issues across 219 teams


Force Git Pull to Overwrite Local Files

It is possible to force a git pull to overwrite local files. However, any uncommitted local changes to tracked files will be lost. Local files that are not tracked by Git will not be affected.

To force a git pull to overwrite local files, first update all origin/<branch> refs to the latest:

git fetch --all

It is recommended to backup your current branch (e.g. master) before resetting:

git branch backup-master

Jump to the latest commit on origin/master and checkout those files:

git reset --hard origin/master

Maintain Current Local Commits

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
We use cookies to personalize content
run ads, and analyze traffic.