Fix "Your local changes to the following files would be overwritten by merge" in Git
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
- error: Your local changes to the following files would be overwritten by checkout:
- error: The following untracked working tree files would be overwritten by merge: config/local.json Please move or remove them before you merge.
- error: Your local changes would be overwritten by cherry-pick.
- error: cannot pull with rebase: You have unstaged changes.
- error: Your local changes to the following files would be overwritten by switch:
You have uncommitted changes in files that the git pull (or merge, checkout) needs to modify, and Git refuses to overwrite them. Put them aside with git stash, pull, then git stash pop, or commit them first; git pull --rebase --autostash does it all in one command.
What this error means
A merge, pull, checkout or cherry-pick rewrites the files in your working copy to match the new commit. If one of those files has local changes that are not in any commit, overwriting them would lose them for good. So Git checks before starting, lists the affected files and stops (Aborting) without touching anything: your repository is exactly as it was. The "untracked working tree files" variant means an untracked file on your side has the same name as a file the remote commit adds.
Common causes
- Uncommitted work in progress on a file a teammate also changed and pushed.
- File changed automatically:
package-lock.jsonafternpm install, a config file rewritten by the IDE or a build tool. - Untracked file at the same path as a file newly added to the repository (e.g. a
.env.exampleor aconfig/local.jsonyou had created by hand). - Line endings or file mode changed:
core.autocrlfor achmodmake files appear modified even though you did not edit them.
Step-by-step fix
-
See what is modified
Look at the list and the content of the changes to decide whether you want to keep them.
git status git diff src/app.js -
Option 1: commit, then pull
If the work is ready, commit it, then fetch the remote and replay your commit on top.
git add src/app.js git commit -m "Update app" git pull --rebase -
Option 2: put aside with stash
The stash saves your changes, you pull, then you reapply them.
-ualso includes untracked files. Ifgit stash popcauses a conflict, the stash is not deleted: resolve the conflict, thengit stash drop.git stash push -u -m "wip before pull" git pull git stash pop -
Option 3: all in one command
--autostashdoes the stash, the pull and the pop for you. You can enable it by default for rebases and merges.git pull --rebase --autostash # make it the default git config --global rebase.autoStash true git config --global merge.autoStash true -
Option 4: discard the local changes
Only if you are sure you do not need them:
git restoreresets the file to the last committed version, and what you wrote is lost for good. When in doubt, prefer the stash.git restore src/app.js git pull -
Untracked files case
Move or rename the blocking file, pull, then compare both versions. If you want to clean up, first preview what would be deleted with
git clean -n:git clean -fdeletes for good.mv config/local.json config/local.json.bak git pull git diff --no-index config/local.json.bak config/local.json
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Run
git statusbefore each pull, and commit often in small steps instead of keeping a lot of uncommitted work. - Put generated or machine-specific files in
.gitignore(.env, caches, local IDE configuration). - Enable
rebase.autoStashso Git automatically stashes and reapplies your changes during agit pull --rebase.
Frequently asked questions
Did I lose my changes?
No. Git stopped before touching anything (Aborting): your files are exactly as they were before the command.
How do I force the pull and overwrite my local files?
With git fetch origin then git reset --hard origin/main. This command erases every uncommitted change and every unpushed local commit: run git stash or create a backup branch first.
Where are my changes after git stash?
In the stash stack. git stash list shows them, git stash show -p shows their content and git stash pop reapplies them.
Useful tools for this error
More Git & GitHub errors
- Fix "failed to push some refs" (! [rejected] fetch first) in Git
- Fix "fatal: refusing to merge unrelated histories" in Git
- Fix "Permission denied (publickey)" with Git and GitHub
- Get out of "detached HEAD" state in Git without losing commits
- Resolve "CONFLICT (content): Merge conflict" and "Automatic merge failed" in Git
- Fix "Support for password authentication was removed" on GitHub
- Fix "GH001: Large files detected" (file over 100 MB) on GitHub