Gitのcannot pull with rebaseエラーの直し方
git pull で error: cannot pull with rebase: You have unstaged changes. と出たら、pull.rebase を true にしたリポジトリに、コミットしていない変更が残っています。ほかの人が push した変更を git pull で取り込むときに出るエラーです。変更を git stash で退避してから git pull するか、git pull --autostash を使うと、エラーは消えて取り込めます。以下では、エラーを実際に起こしてから直すまでを、検証環境の出力そのままで載せます。
検証環境は Ubuntu 24.04、Git 2.43.0 です。リモートは同じ検証環境の中に作ったリポジトリで、別のクローンから notes.txt の1行目を書き換えるコミットを push しておき、手元のクローンで git config pull.rebase true にしてから試しました。出力に出る作業用フォルダーのパスは /path/to/work に置き換えています。GitHub などのホスティングサービス、Windows・macOS、pull.rebase の merges や interactive の値は試していません。
エラーの全文と、取り込まれずに止まること
手元の notes.txt の最後に1行足して、コミットしないまま git pull を実行すると、次のエラーになりました。終了コードは 128 です。
$ 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
エラーの意味は「リベースでの pull はできません。ステージしていない変更があります。コミットするか、stash してください」です。
このとき、リモートからの取得もされていません。エラーには From の行が出ず、手元にある origin/main(リモート追跡ブランチ)は、ほかのクローンが push した ca1877d ではなく、最初のコミットの 1cf2fca のままでした。手元の変更も消えずに残っています。
$ 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
Git 2.43.0 のソース builtin/pull.c を見ると、リベースで取り込む設定で --autostash を使わないときは、取得(fetch)よりも前に、コミットしていない変更がないかを確かめ、あれば止めるようになっています。
git add した変更でも、文面が変わって止まる
変更を git add してステージしてあっても、コミットしていなければ止まります。文面は Your index contains uncommitted changes. に変わりました。
$ 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
ステージした変更と、ステージしていない変更の両方があるときは、3行になります。
$ 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
この3種類の文面は、Git のソース wt-status.c にある関数 require_clean_work_tree で組み立てられています。
どんな変更があると止まるのか
リモートの変更とは関係のないファイルを書き換えただけでも止まりました。取り込むコミットが触っているのは notes.txt だけですが、README.md を書き換えた状態で試すと、同じエラーです。
$ 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
一方で、Git が管理していないファイル(git status --short で ?? と出るもの)があるだけなら、止まらずに取り込めました。
$ 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
比べるために、pull.rebase を false(マージで取り込む)にして試しました。README.md だけを書き換えた状態では、エラーにならずに取り込めて、README.md の変更も残りました。
$ 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
マージで取り込む場合も、取り込むコミットが変えている notes.txt を手元でも書き換えていると止まります。ただし、エラーは別の文面で、取得は済んでいます(この出力は、端末で打ったときと同じ順に表示されるようにして記録したものです。進み具合の表示は出さないようにし、終了コードの行は省いています)。
$ 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
リベースで取り込む設定では、どのファイルかに関係なく、コミットしていない変更があれば取得の前に止まる、という違いがあります。
git stash で退避して、pull のあとに戻す
エラーのメッセージにある「stash してください」のとおりに、変更を退避してから取り込みます。
$ 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
エラーは出ずに取り込めました。続けて git stash pop で、退避した変更を戻します。
$ 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)
リモートの変更(1行目の (other))と、手元の変更(4行目)が、どちらも入っています。
git pull –autostash なら、退避と戻しが1回で済む
git pull --autostash を使うと、退避して、取り込んで、戻すまでを Git がまとめて行います。git pull の公式リファレンスにも、作業の前に一時的な stash を作り、終わったあとに戻すと書かれています。

$ 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)
Created autostash で退避し、Applied autostash. で戻しています。git stash list には何も残っていません。
手元に push していないコミットもある場合は、リベースになりました。この出力には、進み具合の Rebasing (1/1) を同じ行に書き直すための制御文字が入っているので、ここではその表示を省いて載せます。
$ 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
手元のコミットが、取り込んだ ca1877d の後ろに積み直され、notes.txt の変更も戻っています。
毎回付けたくないなら rebase.autoStash を true にする
--autostash を毎回付けなくても済むように、設定で決めておけます。git config の rebase.autoStash の説明にあるとおり、既定は false です。true にすると、何も付けない git pull で同じ動きになりました。
$ 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
変更をコミットしてから pull する
変更がひと区切りついているなら、先にコミットしてしまえば、そのまま git pull できます。手元のコミットは、取り込んだコミットの後ろに積み直されました(ここでも Rebasing (1/1) の表示は省いています)。
$ 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)
autostash を戻すときに同じ行がぶつかった場合
手元の変更と、取り込んだ変更が同じ行を書き換えていると、autostash を戻すところで競合しました。手元で notes.txt の1行目を line1 (app) にしてから git pull --autostash を実行した結果です。
$ 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
取り込みは済んでいて、notes.txt は競合した状態(UU)です。退避した変更は stash@{0}: autostash として残っています。
競合しても、git pull の終了コードは 0 でした。スクリプトの中で使うと、成功したように見えて先に進んでしまいます。Applying autostash resulted in conflicts. が出ていないか、git status で UU のファイルがないかを確かめてください。
ファイルには、取り込んだ側(Updated upstream)と、退避した側(Stashed changes)の両方が入っています。
$ cat notes.txt
<<<<<<< Updated upstream
line1 (other)
=======
line1 (app)
>>>>>>> Stashed changes
line2
line3
ファイルを残したい内容に書き直し、git restore --staged で競合の状態を解くと、ふつうの「変更あり」に戻りました。最後に、残っている stash を git stash drop で消します。git stash の公式リファレンスの git stash pop の説明にも、競合したときは stash が一覧から消えないので、手で解決したあとに git stash drop を実行するよう書かれています。
$ 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





