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

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
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:
git pull --autostash
Keep Your Changes: Commit First
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
# 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.
Leave a Reply