How to Force Git Pull and Overwrite Local Changes
The Problem
When attempting to update a repository on a test machine that hasn’t been synced for a long time, you may encounter the following error:
error: Your local changes to the following files would be overwritten by merge:
app/client.py
...
configs/file.yml
Please commit your changes or stash them before you merge.
Aborting
Because you have made direct modifications to the code for debugging purposes, Git prevents you from performing a standard git pull. In scenarios like testing environments, you may simply want to discard those local changes and force the repository to match the latest version from the remote.
The Solution
To force your local repository to match the remote, execute the following commands:
git fetch
git branch backup-master
git reset --hard origin/master
The first command fetches the latest updates from the remote. The second command creates a backup branch of your current (modified) state, and the third command forces your local branch to match origin/master.
If you are still unsure or if the repository is not fully updated, you can follow up with:
git reset --hard HEAD
git pull
Summary
Modifications on test machines are often limited to minor configurations, such as server addresses or thread counts. If these changes are not critical, overwriting them is the most efficient workflow.
However, if your local changes are valuable, consider stashing them before performing the merge:
git fetch
git stash
git merge '@{u}'
git stash pop
For more information, refer to these resources: