Fixing “Your local changes would be overwritten by merge” in git pull
If git pull prints error: Your local changes to the following files would be overwritten by merge:, a file that changed on the remote is also modified in your working copy, and you have not committed that change yet. This error appears when you try to bring the latest changes from a shared repository and they collide with a file you are working on. Git stops without changing your working folder or branch, so it does not lose your local changes. You can clear the error by setting the changes aside with git stash, committing them first, or discarding them if you do not need them and then running git pull again.
I tested on Ubuntu 24.04 with Git 2.43.0. For the remote, I used a bare repository (origin.git) created on the same VM. Another clone had changed line 1 of settings.ini from port=3000 to port=8080 and pushed it, and I started from a state where the local side had not yet pulled that change. /path/to/work in the output stands in for the actual folder path on the test machine. I did not test interaction with hosting services such as GitHub, or behavior on Windows or macOS.
The full error and why Git stops
Locally, I changed line 3 of the same settings.ini from theme=light to theme=dark. I did not commit it. Running git pull in this state produced the following error.
$ sed -i "s/^theme=light/theme=dark/" settings.ini; git status --short
M settings.ini
$ git pull; echo "exit=$?"
From /path/to/work/origin
1640bcf..75ad638 main -> origin/main
Updating 1640bcf..75ad638
error: Your local changes to the following files would be overwritten by merge:
settings.ini
Please commit your changes or stash them before you merge.
Aborting
exit=1
The message means: "Your local changes to the following files would be overwritten by the merge. Please commit your changes or stash them before you merge. Aborting." The indented line after the first one lists the file that conflicts.

The changed lines do not overlap: the remote changed line 1 and I changed line 3. Git still stops. It checks for uncommitted changes per file, not per line. The git merge reference under "PRE-MERGE CHECKS" also states that when uncommitted changes overlap with files that git pull or git merge would update, the command stops without doing anything.
After the error, the local branch (main) was still at its original commit, and the local change to settings.ini was still there. Only origin/main had moved to the new remote commit. So git pull completed the fetch from the remote and then stopped at the merge.
$ git log --oneline -1; git log --oneline -1 origin/main; git status --short; cat settings.ini
1640bcf 最初のコミット
75ad638 port を 8080 に変更
M settings.ini
port=3000
log=info
theme=dark
I also ran git add on the file and then git pull. It stopped with the same error. (The From line does not appear because the fetch had already completed in the previous git pull.) Trying git merge origin/main on the already-fetched origin/main produced the same message word for word.
$ git add settings.ini; git status --short
M settings.ini
$ git pull; echo "exit=$?"
Updating 1640bcf..75ad638
error: Your local changes to the following files would be overwritten by merge:
settings.ini
Please commit your changes or stash them before you merge.
Aborting
exit=1
In contrast, when I changed only README.md, which had not changed on the remote, git pull went through and my README.md change was kept.
$ echo "メモ" >> README.md; git status --short
M README.md
$ git pull; echo "exit=$?"
From /path/to/work/origin
1640bcf..75ad638 main -> origin/main
Updating 1640bcf..75ad638
Fast-forward
settings.ini | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
exit=0
To keep local changes, stash them and then pull
As the message suggests ("stash them"), set the local changes aside with git stash, run git pull, and then restore them with git stash pop.
$ sed -i "s/^theme=light/theme=dark/" settings.ini
$ git stash; echo "exit=$?"
Saved working directory and index state WIP on main: 1640bcf 最初のコミット
exit=0
$ git status --short; cat settings.ini
port=3000
log=info
theme=light
$ git pull; echo "exit=$?"
From /path/to/work/origin
1640bcf..75ad638 main -> origin/main
Updating 1640bcf..75ad638
Fast-forward
settings.ini | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
exit=0
After git stash, settings.ini returned to the committed local state, so the pull succeeded without the error. Running git stash pop next restored the local change.
$ git stash pop; echo "exit=$?"
Auto-merging settings.ini
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: settings.ini
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (e9366733bedc9a391b95a1f893879efc54620464)
exit=0
$ git log --oneline -1; git status --short; cat settings.ini; git stash list
75ad638 port を 8080 に変更
M settings.ini
port=8080
log=info
theme=dark
Both the remote change (port=8080) and the local change (theme=dark) are now present, and the local part remains an uncommitted change.
git pull –autostash does it in one step
Adding --autostash to git pull stashes the changes, pulls, and reapplies them in one command. As described for --autostash in the git pull reference, the output showed Created autostash and Applied autostash.
$ git pull --autostash; echo "exit=$?"
From /path/to/work/origin
1640bcf..75ad638 main -> origin/main
Updating 1640bcf..75ad638
Created autostash: 9e7b7e6
Fast-forward
settings.ini | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Applied autostash.
exit=0
The result matched running git stash followed by git stash pop: settings.ini contained both port=8080 and theme=dark, and git stash list was empty.
If you changed the same line, restoring conflicts
When I also changed line 1 locally to port=5000, git stash and git pull still went through, but git stash pop hit a conflict.
$ sed -i "s/^port=3000/port=5000/" settings.ini
$ git stash -q; git pull -q; echo "exit=$?"
exit=0
$ git stash pop; echo "exit=$?"
Auto-merging settings.ini
CONFLICT (content): Merge conflict in settings.ini
On branch main
Your branch is up to date with 'origin/main'.
Unmerged paths:
(use "git restore --staged <file>..." to unstage)
(use "git add <file>..." to mark resolution)
both modified: settings.ini
no changes added to commit (use "git add" and/or "git commit -a")
The stash entry is kept in case you need it again.
exit=1
$ git status --short; cat settings.ini; git stash list
UU settings.ini
<<<<<<< Updated upstream
port=8080
=======
port=5000
>>>>>>> Stashed changes
log=info
theme=light
stash@{0}: WIP on main: 1640bcf 最初のコミット
Both the remote line (Updated upstream) and the local line (Stashed changes) were written into settings.ini. The stashed change remains as stash@{0}. As the git stash reference describes for pop, the entry is not removed when a conflict occurs. Edit the file by hand to resolve it, then delete the entry with git stash drop.
In the same situation, git pull --autostash reported Applying autostash resulted in conflicts., and settings.ini ended up in the same state as above.
$ sed -i "s/^port=3000/port=5000/" settings.ini
$ git pull --autostash; echo "exit=$?"
From /path/to/work/origin
1640bcf..75ad638 main -> origin/main
Updating 1640bcf..75ad638
Created autostash: 4bc4554
Fast-forward
settings.ini | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Applying autostash resulted in conflicts.
Your changes are safe in the stash.
You can run "git stash pop" or "git stash drop" at any time.
exit=0
$ git status --short; cat settings.ini; git stash list
UU settings.ini
<<<<<<< Updated upstream
port=8080
=======
port=5000
>>>>>>> Stashed changes
log=info
theme=light
stash@{0}: autostash
In a script that uses git pull --autostash, the exit code was 0 even when the local changes could not be reapplied. If the script continues, it will use a file that contains <<<<<<< markers. Check for resulted in conflicts in the output, or check for UU in git status --short.
Commit the local changes first, then pull
If your local changes are in a finished state, commit them and then run git pull. Now both your side and the remote side have new commits. With Git 2.43.0 and no pull.rebase setting, git pull stopped and asked you to choose how to reconcile the branches.
$ sed -i "s/^theme=light/theme=dark/" settings.ini; git commit -qam "テーマを dark に変更"
$ git pull; echo "exit=$?"
From /path/to/work/origin
1640bcf..75ad638 main -> origin/main
hint: You have divergent branches and need to specify how to reconcile them.
(hint の行は略)
fatal: Need to specify how to reconcile divergent branches.
exit=128
Adding --no-rebase (merge) or --rebase let the pull complete. --no-rebase created a merge commit.
$ git pull --no-rebase; echo "exit=$?"
Auto-merging settings.ini
Merge made by the 'ort' strategy.
settings.ini | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
exit=0
$ git log --oneline --graph -4; cat settings.ini
* 368e53d Merge branch 'main' of /path/to/work/origin
|\
| * 75ad638 port を 8080 に変更
* | 5bc18b1 テーマを dark に変更
|/
* 1640bcf 最初のコミット
port=8080
log=info
theme=dark
With --rebase, the local commit was replayed on top of the remote commit, producing a single linear history.
$ sed -i "s/^theme=light/theme=dark/" settings.ini; git commit -qam "テーマを dark に変更"
$ git pull --rebase; echo "exit=$?"
From /path/to/work/origin
1640bcf..75ad638 main -> origin/main
Rebasing (1/1)
Successfully rebased and updated refs/heads/main.
exit=0
$ git log --oneline --graph -3; cat settings.ini
* d0a8146 テーマを dark に変更
* 75ad638 port を 8080 に変更
* 1640bcf 最初のコミット
port=8080
log=info
theme=dark
In both cases, settings.ini ended up with both port=8080 and theme=dark.
If you do not need the local changes, discard them with git restore and then pull
If the change was just a test edit that you do not need, check its contents with git diff, discard it with git restore, and run git pull again.
$ sed -i "s/^theme=light/theme=dark/" settings.ini; git diff
diff --git a/settings.ini b/settings.ini
index ddeec53..fae48d2 100644
--- a/settings.ini
+++ b/settings.ini
@@ -1,3 +1,3 @@
port=3000
log=info
-theme=light
+theme=dark
$ git restore settings.ini; git status --short
$ git pull; echo "exit=$?"
From /path/to/work/origin
1640bcf..75ad638 main -> origin/main
Updating 1640bcf..75ad638
Fast-forward
settings.ini | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
exit=0
$ cat settings.ini
port=8080
log=info
theme=light
A change discarded with git restore is not kept in any commit or stash. If you are unsure, it is safer to stash it instead of discarding it.
With pull.rebase set to true, the message is different
When git config pull.rebase true is set so that git pull integrates by rebasing, the message for the same situation changes.
$ git config pull.rebase true; sed -i "s/^theme=light/theme=dark/" settings.ini
$ git pull; echo "exit=$?"
error: cannot pull with rebase: You have unstaged changes.
error: Please commit or stash them.
exit=128
This message also appeared when I changed only README.md, which had not changed on the remote.
$ git config pull.rebase true; echo "メモ" >> README.md; git status --short
M README.md
$ git pull; echo "exit=$?"
error: cannot pull with rebase: You have unstaged changes.
error: Please commit or stash them.
exit=128
The fixes are the same. With pull.rebase set to true, you can stash, pull, and then git stash pop; use git pull --autostash; or commit first and then pull. Each of these left settings.ini with both port=8080 and theme=dark.





