How to fix error: Your local changes to the following files would be overwritten by merge in Git
This error occurs when Git detects uncommitted changes in your working directory that conflict with incoming changes from a merge. You need to either commit, stash, or discard your local changes before Git can proceed with the merge.
Quick fix
Fixes error: Your local changes to the following files would be overwritten by merge
git status- Check which files have local changes
- Option 1: Stash your changes (recommended)
- Option 2: Commit your changes first
- Option 3: Discard your local changes
git statuserror: Your local changes to the following files would be overwritten by merge
What this error means
When you run `git pull` or `git merge`, Git attempts to combine the remote branch's changes with your local branch. However, if you have uncommitted modifications to files that are also modified in the incoming changes, Git cannot safely complete the merge without potentially losing your work. Git is being protective here—it refuses to overwrite your uncommitted changes because there's no way to recover them if they're lost. This is intentional behavior, not a bug. The error message lists exactly which files have conflicting local changes. This situation commonly occurs when: - You've made local edits but haven't committed them yet - A teammate pushed changes to the same files you're working on - You're switching branches or pulling updates in the middle of active development - CI/CD pipelines pull changes into a working directory that has been modified
Typical symptoms
- git pull fails immediately without fetching or merging
- git merge aborts before completing
- The error lists specific filenames that have local modifications
- git status shows modified files in your working directory
- The operation is blocked even if local changes seem unrelated
Common causes
- Uncommitted local modifications conflict with incoming remote changes (most common)
- Staged changes that haven't been committed yet
- Local edits made after the last commit but before pulling
- Automatic file modifications (IDE config, generated files, package-lock.json)
- Interrupted previous merge leaving working directory in inconsistent state
How to fix "error: Your local changes to the following files would be overwritten by merge"
First, understand what files Git is protecting:
git statusThis shows all modified, staged, and untracked files. The files listed in the error message will appear here as "modified" or "staged".
If you want to keep your local changes and apply them after the merge, use git stash:
# Save your changes temporarily
git stash
# Now pull/merge will work
git pull
# Restore your changes
git stash popIf there are conflicts when popping the stash, Git will notify you and you can resolve them manually. Your stashed changes are still safe in the stash list even if pop fails.
To see what's in your stash:
git stash list
git stash show -p stash@{0}If your changes are ready to be saved permanently, commit them before merging:
git add .
git commit -m "WIP: Save local changes before merge"
git pullIf there are conflicts after pulling, Git will enter merge conflict resolution mode where you can address each conflict individually. This is often cleaner than resolving stash conflicts.
If you don't need your local changes, you can discard them:
Discard changes to specific files:
git checkout -- path/to/file.txtDiscard all uncommitted changes:
git checkout -- .Or using the newer restore command (Git 2.23+):
git restore path/to/file.txt
git restore .After discarding, you can proceed with your merge or pull.
For a streamlined workflow, use autostash which automatically stashes before rebasing and pops after:
git pull --rebase --autostashThis is equivalent to:
git stash
git pull --rebase
git stash popTo make this the default behavior:
git config --global rebase.autoStash trueAs a last resort, if you want to completely discard local changes and match the remote:
git reset --hard HEAD
git pullWarning: This permanently deletes all uncommitted changes. Only use this if you're certain you don't need your local modifications.
For an even more aggressive reset to match remote exactly:
git fetch origin
git reset --hard origin/mainAdvanced notes
### Dealing with Generated Files
If the conflict is in files like package-lock.json, .idea/, or other generated files that you don't manually edit:
# Discard only the generated files
git checkout -- package-lock.json
git pull
npm install # Regenerate lock fileConsider adding generated files to .gitignore if they shouldn't be tracked.
### Partial Stash
If you only want to stash specific files:
git stash push -m "Stashing specific files" -- path/to/file1.txt path/to/file2.txt
git pull
git stash pop### Stash Including Untracked Files
By default, git stash only saves tracked files. To include untracked files:
git stash -u # Include untracked files
git stash -a # Include all files (untracked + ignored)### Viewing Diff Before Deciding
Before choosing how to handle your changes, review what you've modified:
# See what you changed locally
git diff
# See what's coming from remote
git fetch origin
git diff HEAD..origin/main### CI/CD Pipelines
In CI/CD environments, this error often occurs when the pipeline workspace isn't clean. Add a cleanup step:
git clean -fd # Remove untracked files
git checkout -- . # Discard modifications
git pullOr configure your CI to always start with a fresh clone.
Frequently asked questions
- What does "error: Your local changes to the following files would be overwritten" mean?
- When you run git pull or git merge, Git attempts to combine the remote branch's changes with your local branch. However, if you have uncommitted modifications to files that are also modified in the incoming changes, Git cannot safely complete the merge without potentially losing your work. Git is being protective here—it refuses to overwrite your uncommitted changes because there's no way to recover them if they're lost.
- How do I fix "error: Your local changes to the following files would be overwritten"?
- This error occurs when Git detects uncommitted changes in your working directory that conflict with incoming changes from a merge. You need to either commit, stash, or discard your local changes before Git can proceed with the merge.
- What causes "error: Your local changes to the following files would be overwritten"?
- Uncommitted local modifications conflict with incoming remote changes (most common) Staged changes that haven't been committed yet Local edits made after the last commit but before pulling
- Is the "error: Your local changes to the following files would be overwritten" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚Git Official Documentation
- 📖Git stash documentation(Official Docs)
- 📖Git merge documentation(Official Docs)
- 📖Git pull documentation(Official Docs)
- 💬
- 💬How to address local changes would be overwritten by merge(Stack Overflow)