git force pull / overwrite local changes — how to do it safely
Quick Answer
# If you might need your local work, save it first
git stash push -u -m "before overwrite"
# Fetch the latest remote state
git fetch origin
# Make your current branch match the remote exactly
git reset --hard origin/main
When this happens
You want to force pull from the remote and overwrite local changes so your branch matches origin/main exactly.
error: Your local changes to the following files would be overwritten by merge:
src/app.js
Please commit your changes or stash them before you merge.
Aborting
Git refuses to pull because you have local changes or commits that would be overwritten. If you truly want the remote version, fetch first and then reset hard to the remote branch.
Safer variants
Keep a backup of your local work before overwriting
# Save tracked + untracked files
git stash push -u -m "before overwrite"
# Later, restore if needed
git stash pop
Also remove untracked files and folders
A hard reset does not delete untracked files. If you want a completely clean tree, run git clean after the reset.
git fetch origin
git reset --hard origin/main
git clean -fd
Overwrite a branch other than main
# Example: make your branch match origin/develop
git fetch origin
git reset --hard origin/develop
If you already pushed your local commits
Do not reset shared history unless you really intend to rewrite it. In shared branches, use git revert or coordinate before force-pushing.
# Safer for shared history
git revert <commit>
# Only if you intentionally rewrite remote history
git push --force-with-lease origin main
Related