Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readGit
#Git#Merging#Working Tree#CLI
A Git working tree crate with two amber edited files colliding at a cyan merge gate, a stash drawer beside it

Introduction & Quick Summary

Git stops before merging anything:

code
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. Aborting

This 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.

bash
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 add

A reproducible setup needs a file that both sides changed. On a scratch clone:

bash
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 here

The message names the command that was blocked, so the resolution is the same regardless of verb:

CommandMessage ends withWhat was blocked
git mergePlease commit your changes or stash themmerge commit
git pullsame as mergefetch already happened
git checkout <branch>would be overwritten by checkoutbranch switch
git rebasecannot rebase: You have unstaged changesreplay
git cherry-pickYour local changes would be overwrittensingle commit apply

Key Comparison Table

ResponseKeeps editsRiskUse when
git stash then popyes, in a stash entrypop conflictsmall, unrelated edits
Commit to a temp branchyes, in historybranch clutterwork in progress that matters
git restore the filenodestroys editsgenerated files or accidental changes
--autostashyesconflict on restoreroutine pulls of clean-history repos
git worktreeyesextra directoryhotfix needed while the tree is dirty
Stash including untrackedyeslarge node_modules if unignorednew 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.

bash
git diff --stat git diff --ignore-space-at-eol --stat git diff --summary | head

If 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.

bash
git stash push --include-untracked -m "before merge" git merge origin/main git stash pop

If 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.

bash
git pull --rebase --autostash # or per-repository default git config rebase.autoStash true

Check 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.

bash
git switch -c wip/config-tweak git commit -am "wip: config tweak" git switch main git merge origin/main

The 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.

bash
git status --porcelain | grep '^??' mv docs/new.md /tmp/new.md.bak git merge origin/main

Read 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.

bash
git worktree add ../hotfix origin/main cd ../hotfix git switch -c hotfix/login-timeout

When 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.

bash
git ls-files src/config.ts # tracked generated file? git rm --cached src/config.ts printf 'src/config.ts\n' >> .gitignore

Untracked 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 list becomes unreadable, and entries get dropped by accident. Use -m and keep the list short.
  • Forgetting untracked files. git stash alone 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 --hard under 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.
SymptomCauseFix
file is dirty but diff is emptymode or line endingcore.fileMode, .gitattributes
refusal persists after stashinguntracked file at that path--include-untracked
stash pop conflicts every timeedits overlap merged linescommit to a branch instead
pull aborted but objects fetchedfetch precedes mergemerge 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 documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.