error: Your local changes to the following files would be overwritten by merge: —
the commits you are pulling change a file that you have edited and not committed, and Git will
not overwrite your edits. Commit them, or put them aside with git stash, pull, and
bring them back with git stash pop.
Nothing was changed by the failed pull: your edits are still in the file and the incoming commits are fetched but not applied. Stash when the edits are not ready for a commit; commit when they are.
The error
PS> git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 279 bytes | 13.00 KiB/s, done.
From ../origin
fd2c89c..16059d0 main -> origin/main
Updating fd2c89c..16059d0
error: Your local changes to the following files would be overwritten by merge:
notes.txt
Please commit your changes or stash them before you merge.
Aborting
Exit code 1. When your branch also has local commits, the last line is followed by
Merge with strategy ort failed. and the exit code is 2; the cause is the same.
Why it happens
git pull is a fetch followed by a merge, here a fast-forward. Before the merge
writes anything, Git checks every file it is about to change. A teammate's commit changed line 1
of notes.txt; you had edited line 10 of the same file and not committed. Git merges
commits, not loose edits in your working folder, so writing the incoming version of
notes.txt would have destroyed your change. It stops instead, and lists only the
files that both sides touched. A file you edited that the pull does not change never blocks it.
Both fixes work for the same reason: they turn your edit into something Git can merge.
git commit makes it a commit; git stash saves it as a commit-like entry
and cleans the file, so the pull runs, and git stash pop merges the saved edit back.
The fix
Stash, pull, pop:
PS> git stash
Saved working directory and index state WIP on main: fd2c89c Add notes
PS> git pull
Updating fd2c89c..16059d0
Fast-forward
notes.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
PS> git stash pop
Auto-merging notes.txt
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: notes.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (e8217d661183f8ea5632b0243c2569f27f537eff)
Auto-merging notes.txt and Dropped mean the edit came back cleanly and
the stash entry is gone. git diff afterwards showed only the local line 10 change, on
top of the teammate's commit. Or commit first and pull:
$ git commit -am 'Edit line 9'
[main 087f63c] Edit line 9
1 file changed, 1 insertion(+), 1 deletion(-)
$ git pull
Auto-merging notes.txt
Merge made by the 'ort' strategy.
notes.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
The file then held both people's lines, and the merge was pushed. Stash versus discard:
git stash keeps your edits and gives them back with pop, while
git restore notes.txt throws them away for good, so use it only for edits you do not
want. The two edits here touched different lines. When a second test edited the same line on both
sides, git stash pop printed CONFLICT (content): Merge conflict in notes.txt
and kept the stash entry: resolve the markers in the file, then run git stash drop.
How it was reproduced
Git for Windows 2.45.2 (git version 2.45.2.windows.1). A local bare repository
stood in for the remote, so the output says ../origin where you would see a URL. Two
clones with fictional identities shared a ten-line notes.txt: one changed line 1 and
pushed, the other edited line 10 without committing and ran git pull. The error and
the stash fix were captured in a Windows PowerShell 5.1 console; the commit fix and the
local-commit variant were run in Git Bash 5.2.26 with the same messages. The personal global
config was replaced by an empty file (GIT_CONFIG_GLOBAL).
Frequently asked
- How do I git pull without losing my local changes?
- Run git stash, then git pull, then git stash pop. The stash saves your uncommitted edits and cleans the files, the pull applies the incoming commits, and pop merges your edits back on top. Or commit the edits first and pull, which makes a merge.
- Is it safe to use git stash before git pull?
- Yes. git stash keeps your edits in a stash entry until you pop or drop it, and git stash list shows what is stored. It is the opposite of git restore, which discards edits permanently.
- What does 'untracked working tree files would be overwritten by merge' mean?
- The pull brings in a file that exists in your folder but is not tracked by Git. Git will not overwrite it. Move or rename your copy, pull, then compare the two versions.
More decoded errors in the Fixes category. The same check stops a branch switch too: your local changes would be overwritten by checkout.