[Git] Error: Your local changes would be overwritten by merge — Prevent Data Loss in Git — How to Fix It
Summary
error: Your local changes to the following files would be overwritten by merge occurs when you try to merge or pull while having uncommitted local modifications. Git refuses to proceed to avoid overwriting your changes. To fix this, you must commit, stash, or discard those local edits before continuing.
Context
This message appears during git pull, git merge, or git checkout operations when local changes exist in files that Git needs to modify or replace. The safeguard prevents data loss — Git stops automatically merging or switching branches until you resolve the conflict between your local working directory and the incoming changes.
Probable Cause
- Uncommitted edits: You modified files locally that also changed in the remote branch.
- Unstaged or staged changes overlap: The same files are touched by both local and remote commits.
- Pending merges: You previously interrupted a merge without committing or aborting it.
- Pulling before commit: Trying to synchronize before saving your local progress.
Quick Fix
- Check which files are blocking the merge:
git status - If you want to keep your work, commit it:
Then retry your pull or merge:git add . git commit -m "Save local changes"git pull origin main - If you’re not ready to commit, stash temporarily:
Perform the pull or merge, then reapply your changes:git stash push -m "temp"git stash pop - If your local modifications don’t matter, discard them:
git restore . # for Git 2.23+ # or git checkout -- . # for older versions
Example
# Error $ git pull origin main 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
Fix
$ git stash push -m "temp"
Saved working directory and index state WIP on main: 7f6e1a2 Add footer section
$ git pull origin main
Updating 7f6e1a2..a5b2cd8
Fast-forward
$ git stash pop
Auto-merging src/app.js
Pitfalls & Debug
- Symptom → You edited files locally but forgot. Fix → Use
git diffto inspect what changed. - Symptom → You stashed work but nothing happened after
stash pop. Fix → Checkgit stash list— the stash may still exist or failed to apply due to conflicts. - Symptom → You just want to overwrite everything. Fix → Run
git fetchthengit reset --hard origin/main— WARNING: this erases local edits permanently. - Symptom → Merge failed again after stash. Fix → Manually resolve conflicts,
git add ., thengit commit.
Verification & Next Steps
# Verify you have a clean working directory git status
If you stashed
git stash list
If you committed
git log -1
Best Practices
- Always commit or stash before running
git pullorgit merge. - Use feature branches to isolate work and avoid local/remote overlap.
- Keep commits small and frequent — easier recovery if conflicts arise.
- Run
git statusbefore switching branches to avoid surprises.
Sources
Tool/Git, OS/Cross-platform, Topic/Merge Conflicts & Data Safety