Git Pull “Your Local Changes Would Be Overwritten by Merge”: Stash, Commit or Discard

This error means the file you changed locally is also changed on the remote, and Git will not pull over the top of your edits. To fix it, put your changes somewhere safe first: git stash, pull, then git stash pop.

Bash
error: Your local changes to the following files would be overwritten by merge:
	f.txt
Please commit your changes or stash them before you merge.
Aborting
PowerShell: two clones of one repository, a remote edit pushed from one, a local edit in the other, and git pull stopping with Your local changes would be overwritten by merge

Nothing has been lost and nothing has been merged. Git stopped before it touched anything. You have three ways out, depending on whether you want to keep what you changed.

Keep Your Changes: Stash, Pull, Pop

Bash
git stash
git pull
git stash pop

git stash sets your uncommitted changes aside and gives you a clean working folder. git pull then goes through, and git stash pop puts your changes back on top of the new code. If you edited different lines from the remote change, that is the end of it.

If you edited the same lines, the pop stops with a conflict. Git keeps the stash entry in that case, so your work is not lost. Open the file, look for the <<<<<<< and >>>>>>> markers, keep the lines you want, then git add the file and git stash drop to clear the leftover entry.

There is a one-line version that does the same three steps:

Bash
git pull --autostash

Keep Your Changes: Commit First

Bash
git add .
git commit -m "My local change"
git pull

Committing first is the better choice when the change is real work. The pull then merges the remote commits into yours, and Git opens an editor for the merge commit message if both sides moved. Edits to neighbouring lines of the same file can still conflict, so do not be surprised if one shows up. Git marks the file and you resolve it the same way as above.

Throw Your Changes Away

Bash
# see what you are about to lose
git diff

# discard the changes to one file
git restore f.txt

# or discard every uncommitted change to tracked files
git restore .

git pull

Only do this if you know the local edits are not wanted, because there is no undo. git restore needs Git 2.23 or newer; on an older Git the same job is git checkout -- f.txt. Files Git does not track yet are left alone.

Why It Happens

A pull is a fetch followed by a merge. The merge needs to write the remote version of the file into your working folder, and if you have uncommitted changes in that same file it would overwrite them. Git refuses rather than guess. Changes to files the remote did not touch never cause this error, which is why it tends to appear on one or two files and not the whole project.

Common Questions

Does the error mean I have a merge conflict?

Not yet. It stops the pull before the merge starts, so there is no conflict to resolve. A conflict can appear afterwards, when you pop the stash or merge your commit, if both sides edited the same lines.

What if git stash pop says there are conflicts?

Your changes are safe. Git keeps the stash entry when a pop conflicts. Fix the file, run git add on it, then git stash drop once you are happy.

Should I use git pull –rebase instead?

It does not skip this check. Git still wants a clean working folder, so stash or commit first. Rebase just changes how your commits are replayed afterwards.

Related

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *