日本語

Fixing the “cannot pull with rebase” Error in Git

​​Git​command line,Troubleshooting

If git pull prints error: cannot pull with rebase: You have unstaged changes., the repository has pull.rebase set to true and you have uncommitted changes. This error appears when you try to pull changes that someone else pushed. You can either stash the changes with git stash and then run git pull, or run git pull --autostash. Either way, the error goes away and the changes are pulled in. Below, I reproduce the error and show how to fix it, using the output from the test environment as-is.

Note

The test environment is Ubuntu 24.04 with Git 2.43.0. The remote was a repository created inside the same test environment. I pushed a commit from a separate clone that rewrote the first line of notes.txt, then set git config pull.rebase true in my local clone and ran the tests. The work folder path in the output is replaced with /path/to/work. I did not test GitHub or other hosting services, Windows or macOS, or the merges and interactive values of pull.rebase.

The full error, and how it stops before fetching

I added a line to the end of the local notes.txt, did not commit, and ran git pull. It failed with the following error. The exit code was 128.

console
$ echo "line4 (app)" >> notes.txt; git status --short
 M notes.txt

$ git pull; echo "exit=$?"
error: cannot pull with rebase: You have unstaged changes.
error: Please commit or stash them.
exit=128

The message means: "Cannot pull with rebase: you have unstaged changes. Commit or stash them."

At this point, nothing was fetched from the remote. The error output has no From line, and the local origin/main (the remote-tracking branch) was still at the first commit, 1cf2fca, not at ca1877d which the other clone had pushed. The local changes were still there.

console
$ git log --oneline -1; git status --short; git diff
1cf2fca 最初のコミット
 M notes.txt
diff --git a/notes.txt b/notes.txt
index 83db48f..b05b651 100644
--- a/notes.txt
+++ b/notes.txt
@@ -1,3 +1,4 @@
 line1
 line2
 line3
+line4 (app)

$ git rev-parse --short origin/main
1cf2fca

Looking at the Git 2.43.0 source, builtin/pull.c, when pulling with rebase without --autostash, Git checks for uncommitted changes before fetching, and stops if there are any.

Staged changes also stop the pull, with a different message

If you stage the changes with git add but do not commit them, the pull still stops. The message changes to Your index contains uncommitted changes.

console
$ git add notes.txt; git status --short
M  notes.txt

$ git pull; echo "exit=$?"
error: cannot pull with rebase: Your index contains uncommitted changes.
error: Please commit or stash them.
exit=128

When you have both staged and unstaged changes, the message has three lines.

console
$ echo "line5 (app)" >> notes.txt; git status --short
MM notes.txt

$ git pull; echo "exit=$?"
error: cannot pull with rebase: You have unstaged changes.
error: additionally, your index contains uncommitted changes.
error: Please commit or stash them.
exit=128

These three messages are built in the function require_clean_work_tree in Git's wt-status.c.

What kinds of changes stop the pull

The pull stopped even when I only changed a file that the incoming commit does not touch. The incoming commit only changes notes.txt, but when I edited README.md instead, I got the same error.

console
$ echo "README の追記" >> 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

On the other hand, a file that Git does not track (shown as ?? in git status --short) did not stop the pull, and the pull completed.

console
$ echo "memo" > memo.txt; git status --short
?? memo.txt

$ git pull; echo "exit=$?"
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Updating 1cf2fca..ca1877d
Fast-forward
 notes.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
exit=0

For comparison, I set pull.rebase to false (merge on pull). With only README.md modified, the pull succeeded without an error, and the change to README.md was kept.

console
$ git config pull.rebase false; echo "README の追記" >> README.md; git status --short
 M README.md

$ git pull; echo "exit=$?"
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Updating 1cf2fca..ca1877d
Fast-forward
 notes.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
exit=0

$ git log --oneline -2; git status --short
ca1877d other で1行目を変更
1cf2fca 最初のコミット
 M README.md

With merge-based pulls, the pull also stops if you modify notes.txt locally, which the incoming commit also changes. However, the error message is different, and the fetch has already completed. (This output is recorded in the same order it appears in a terminal. I left out the progress display and the exit code line.)

console
$ git config pull.rebase false; echo "line4 (app)" >> notes.txt; git status --short
 M notes.txt

$ git pull
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Updating 1cf2fca..ca1877d
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

So with the rebase setting, any uncommitted change stops the pull before the fetch, regardless of which file it is in.

Stash the changes with git stash, then restore them after the pull

Following the error's advice to stash, I saved the changes first and then pulled.

console
$ echo "line4 (app)" >> notes.txt

$ git stash; echo "exit=$?"
Saved working directory and index state WIP on main: 1cf2fca 最初のコミット
exit=0

$ git pull; echo "exit=$?"
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Updating 1cf2fca..ca1877d
Fast-forward
 notes.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
exit=0

The pull succeeded with no error. Next, git stash pop restores the stashed changes.

console
$ git stash pop; echo "exit=$?"
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} (39d3854c449cb4db585a1d4b61c085ececb715fe)
exit=0

$ git log --oneline -2; git status --short; cat notes.txt
ca1877d other で1行目を変更
1cf2fca 最初のコミット
 M notes.txt
line1 (other)
line2
line3
line4 (app)

Both the remote change ((other) on line 1) and the local change (line 4) are present.

With git pull –autostash, stashing and restoring happen in one step

With git pull --autostash, Git stashes, pulls, and restores in one command. The git pull reference also says it creates a temporary stash before the operation and restores it afterward.

Top: when there are uncommitted changes, the pull stops before fetching. Bottom: the changes are stashed, the remote changes are fetched and integrated, and the stash is restored. If the same lines conflict, the stash remains with conflicts
git pull with pull.rebase set to true, and the flow with –autostash
console
$ echo "line4 (app)" >> notes.txt

$ git pull --autostash; echo "exit=$?"
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Updating 1cf2fca..ca1877d
Created autostash: c918802
Fast-forward
 notes.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Applied autostash.
exit=0

$ git log --oneline -2; git status --short; cat notes.txt; git stash list
ca1877d other で1行目を変更
1cf2fca 最初のコミット
 M notes.txt
line1 (other)
line2
line3
line4 (app)

It printed Created autostash when it stashed, and Applied autostash. when it restored. git stash list is empty afterward.

When there are also local commits that have not been pushed, the pull rebased them. The output below includes control characters that rewrite the Rebasing (1/1) progress line in place, so I've left out that display here.

console
$ echo "README の追記" >> README.md; git commit -q -am "app で README に追記"; echo "line4 (app)" >> notes.txt; git status --short
 M notes.txt

$ git pull --autostash; echo "exit=$?"
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Created autostash: b19b89c
Applied autostash.
Successfully rebased and updated refs/heads/main.
exit=0

$ git log --oneline -3; git status --short; git stash list
6479faf app で README に追記
ca1877d other で1行目を変更
1cf2fca 最初のコミット
 M notes.txt

The local commit was replayed on top of the incoming ca1877d, and the change to notes.txt was restored.

Set rebase.autoStash to true if you don't want to type –autostash every time

You can set this in the config so you don't have to add --autostash each time. As the git config reference for rebase.autoStash says, the default is false. With true, a plain git pull with no options behaved the same way.

console
$ git config rebase.autoStash true; echo "line4 (app)" >> notes.txt

$ git pull; echo "exit=$?"
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Updating 1cf2fca..ca1877d
Created autostash: c918802
Fast-forward
 notes.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Applied autostash.
exit=0

Commit your changes before you pull

If your changes are at a good stopping point, you can commit them first and then run git pull. The local commit was replayed on top of the incoming commit. (The Rebasing (1/1) progress line is again left out.)

console
$ echo "line4 (app)" >> notes.txt; git commit -q -am "app で4行目を追加"; git status --short

$ git pull; echo "exit=$?"
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Successfully rebased and updated refs/heads/main.
exit=0

$ git log --oneline -3; cat notes.txt
9d11174 app で4行目を追加
ca1877d other で1行目を変更
1cf2fca 最初のコミット
line1 (other)
line2
line3
line4 (app)

When the same lines conflict while restoring the autostash

When your local changes and the incoming changes edit the same line, restoring the autostash produces a conflict. This is the result when I changed line 1 of notes.txt to line1 (app) locally and then ran git pull --autostash.

console
$ git pull --autostash; echo "exit=$?"
From /path/to/work/remote
   1cf2fca..ca1877d  main       -> origin/main
Updating 1cf2fca..ca1877d
Created autostash: 45f6eb0
Fast-forward
 notes.txt | 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 log --oneline -2; git status --short; git stash list
ca1877d other で1行目を変更
1cf2fca 最初のコミット
UU notes.txt
stash@{0}: autostash

The pull itself completed, and notes.txt is in a conflicted state (UU). The stashed changes remain as stash@{0}: autostash.

Warning

Even with the conflict, git pull exited with 0. If you use it in a script, it will look like the command succeeded and the script will continue. Check whether Applying autostash resulted in conflicts. is printed, and whether git status shows any UU files.

The file contains both the incoming version (Updated upstream) and the stashed version (Stashed changes).

console
$ cat notes.txt
<<<<<<< Updated upstream
line1 (other)
=======
line1 (app)
>>>>>>> Stashed changes
line2
line3

I rewrote the file with the content I wanted to keep, then ran git restore --staged to clear the conflict state. That returned it to a normal "modified" state. Finally, I removed the leftover stash with git stash drop. The git stash reference, in its description of git stash pop, also notes that when a conflict occurs the stash is not removed from the list, and you should run git stash drop after resolving it by hand.

console
$ printf "line1 (other) (app)\nline2\nline3\n" > notes.txt; git restore --staged notes.txt; git status --short; git diff
 M notes.txt
diff --git a/notes.txt b/notes.txt
index 54a086e..bad41d2 100644
--- a/notes.txt
+++ b/notes.txt
@@ -1,3 +1,3 @@
-line1 (other)
+line1 (other) (app)
 line2
 line3

$ git stash drop; echo "exit=$?"
Dropped refs/stash@{0} (45f6eb077fdad67cf0bc82d4f39272499699a87b)
exit=0

​Posted by 86