How to use git pull force to overwrite local files?

First, update all references to the latest changes on the remote repository:

git fetch --all

To ensure you have a backup of your current branch (e.g., master), create a new branch:

git branch backup-master

Switch to the latest commit on the remote master branch and update your local files:

git reset --hard origin/master

Explanation:

  • git fetch downloads the latest changes from the remote repository without merging or rebasing.
  • git reset --hard resets the local master branch to match the state of the remote master branch, discarding any local changes.

Note: If you want to keep your current local commits, create a new branch from the master branch before resetting:

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

This way, your old commits will be kept in the new branch new-branch-to-save-current-commits.

If you have uncommitted changes, even if they are staged with git add, they will be lost. To prevent this, stash or commit your changes before resetting:

git stash

Later, after resetting, you can reapply these changes:

git stash pop

This may lead to merge conflicts that you’ll need to resolve.

Warning: Any uncommitted local change to tracked files will be lost, even if staged.

But any local file that’s not tracked by Git will not be affected.