Git: local changes would be overwritten by merge
Git refuses a merge, pull, or checkout that would clobber uncommitted edits. Inspect the listed files, then stash, commit, or restore before retrying.

Introduction & Quick Summary
Git stops before merging anything:
error: Your local changes to the following files would be overwritten by merge:
src/config.ts
Please commit your changes or stash them before you merge.
AbortingThis is a protection, not a failure. The incoming merge would need to write src/config.ts, but the working tree already holds uncommitted edits to that same file. Git does not guess which version to keep, so it aborts before touching anything. Nothing is lost yet, and that is the point.
The same refusal appears with different verbs: git pull (which is fetch plus merge), git checkout, git switch, git rebase, and git cherry-pick. Only the last line changes: Please move or remove them before you merge means untracked files are in the way.
Three safe responses:
- Stash the edits, merge, then pop them back and resolve the overlap.
- Commit them to a temporary branch when they are substantial work.
- Discard them only when you are certain the file contents do not matter.
git merge --autostash and git pull --rebase --autostash automate the first option and are useful once you trust the pop step to succeed.
Core Mechanics, Signatures & Reproducible Setup
Git refuses when a path is both dirty in the working tree and modified by the merge base comparison. The check runs before the index is updated, so the merge commit is never created and HEAD does not move. git status after the abort shows the same dirty files with the merge untouched.
git status --porcelain=v1 # machine-readable dirty list
git diff --stat # tracked modifications only
git stash push -m "wip before pull" --include-untracked
git merge origin/main
git stash pop # may conflict; resolve, then git addA reproducible setup needs a file that both sides changed. On a scratch clone:
git clone https://example.com/repo.git demo && cd demo
echo "local" >> README.md
git commit -am "local edit"
git checkout -b other HEAD~1
echo "remote" >> README.md
git commit -am "remote edit"
git checkout main
echo "dirty" >> README.md
git merge other # the refusal appears hereThe message names the command that was blocked, so the resolution is the same regardless of verb:
| Command | Message ends with | What was blocked |
|---|---|---|
git merge | Please commit your changes or stash them | merge commit |
git pull | same as merge | fetch already happened |
git checkout <branch> | would be overwritten by checkout | branch switch |
git rebase | cannot rebase: You have unstaged changes | replay |
git cherry-pick | Your local changes would be overwritten | single commit apply |
Key Comparison Table
| Response | Keeps edits | Risk | Use when |
|---|---|---|---|
git stash then pop | yes, in a stash entry | pop conflict | small, unrelated edits |
| Commit to a temp branch | yes, in history | branch clutter | work in progress that matters |
git restore the file | no | destroys edits | generated files or accidental changes |
--autostash | yes | conflict on restore | routine pulls of clean-history repos |
git worktree | yes | extra directory | hotfix needed while the tree is dirty |
| Stash including untracked | yes | large node_modules if unignored | new files block the merge |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Identify whether edits are real or cosmetic
Line endings and file modes create dirty files with no content change, and those are the ones worth discarding.
git diff --stat
git diff --ignore-space-at-eol --stat
git diff --summary | headIf the only difference is mode (old mode 100644 / new mode 100755), git config core.fileMode false stops Git from reporting it in that repository. If it is CRLF churn, fix core.autocrlf and a .gitattributes entry instead of stashing forever.
2. Stash and pop, including untracked files
Plain git stash leaves untracked files alone, so a merge that adds a file at the same path still fails, now with the untracked variant of the message.
git stash push --include-untracked -m "before merge"
git merge origin/main
git stash popIf the pop conflicts, the stash entry is kept. Resolve the file, git add it, then drop the stash explicitly with git stash drop, so a failed pop does not silently accumulate entries.
3. Autostash for routine pulls
--autostash stashes before the operation and applies after, which removes the stop for small edits. It does not remove the conflict: if the restored edits overlap the merged lines, you get a conflict at the end instead of a refusal at the start.
git pull --rebase --autostash
# or per-repository default
git config rebase.autoStash trueCheck the exit status in scripts. A rebase that reports a conflict after autostash leaves the stash applied and the rebase paused, which is a state no automation should assume away.
4. Move the work to a branch
When the edits are the start of a real change, committing them is cleaner than a stash nobody remembers.
git switch -c wip/config-tweak
git commit -am "wip: config tweak"
git switch main
git merge origin/mainThe branch preserves context, can be pushed as a draft, and cannot be lost by a later git stash clear. This is the default for anything larger than a one-line experiment.
5. Untracked files in the way
When the merge adds docs/new.md and an untracked file of that name exists, Git reports a different line and refuses for the same reason.
git status --porcelain | grep '^??'
mv docs/new.md /tmp/new.md.bak
git merge origin/mainRead the untracked file before deleting it; it may be the only copy of work that was never committed.
6. Working tree needed for an urgent fix
A long-running local experiment should not block a hotfix. A second worktree checks out another branch without disturbing the current files.
git worktree add ../hotfix origin/main
cd ../hotfix
git switch -c hotfix/login-timeoutWhen finished, git worktree remove ../hotfix cleans up. Nothing was stashed and no dirty state was disturbed.
Automation that runs git pull on a dirty tree. A deploy script that pulls in a directory where the application writes files will hit this refusal sooner or later, usually on a generated config. The fix is not --force: it is to stop tracking generated files, or to check out a clean copy for the deploy.
git ls-files src/config.ts # tracked generated file?
git rm --cached src/config.ts
printf 'src/config.ts\n' >> .gitignoreUntracked data that a running service writes belongs outside the repository. When the deploy really must reuse the directory, use git fetch plus git reset --hard origin/main as an explicit step that is understood to discard local state, not as a workaround for a refusal nobody read.
Common Compilation/Runtime Mistakes & Version Gotchas
git checkout -- .to "fix" it. That discards every local edit in the tree, including work unrelated to the conflict. Restore a named file only.- Stash without a message.
git stash listbecomes unreadable, and entries get dropped by accident. Use-mand keep the list short. - Forgetting untracked files.
git stashalone does not cover them; the merge fails again with a nearly identical message. - Dirty submodules. A modified submodule pointer appears as a dirty path; update it in the submodule or commit the pointer before merging.
git reset --hardunder pressure. It resolves the message and destroys uncommitted work. Stash first even when you intend to discard, so the data still exists for a day.
| Symptom | Cause | Fix |
|---|---|---|
file is dirty but diff is empty | mode or line ending | core.fileMode, .gitattributes |
| refusal persists after stashing | untracked file at that path | --include-untracked |
| stash pop conflicts every time | edits overlap merged lines | commit to a branch instead |
| pull aborted but objects fetched | fetch precedes merge | merge locally after resolving |
Compare the local and incoming versions with the text diff tool before choosing which side to keep, and check executable bits with the chmod reference when a mode-only change keeps the tree dirty.
The behaviour is specified in git merge, including --autostash, and the stash mechanics are documented in git stash. Together they cover the states this message can leave behind.
Official references
primary documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: not a git repository (or any parent): .git
The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.
Git: fatal: could not read from remote repository
Git could not authenticate or reach the remote. Check the remote URL, test SSH with -T, verify the key is offered, and use HTTPS on 443 when 22 is blocked.
Git: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.