できない.dev

Git rebase でコンフリクトが解消できない

rebase 中のコンフリクトは適用中のコミット単位で解消する必要があり、通常の merge と勝手が異なる。
中断したい時は git rebase --abort で開始前に戻せる。

#rebase#conflict#merge#history

公開: 更新:

要約

git rebase 中に表示される CONFLICT (content): は、適用中のコミット 1 つに対する衝突。
衝突を解消したら git add してから git rebase --continue で次のコミットに進む。
途中で諦めたい場合は git rebase --abort を打てば、rebase 開始前の状態に戻せる。

よくある原因

  1. コミット単位で何度もぶつかる: rebase は対象コミットを 1 個ずつ再適用するため、同じファイルで衝突が連発することがある
  2. コンフリクトマーカーを残して add: <<<<<<< ======= >>>>>>> を消し忘れたまま git add し、壊れた内容がコミットされる
  3. continue を忘れて commit: 通常通り git commit してしまい、rebase の進行管理が壊れる
  4. abort を知らない: 行き詰まって新しいクローンを作り直してしまう

解決策

1. 標準的な解消フロー

git rebase main
# CONFLICT が出たら
$EDITOR path/to/file        # マーカーを消し、正しい内容に編集
git add path/to/file
git rebase --continue

衝突が複数コミットにまたがる場合は 同じ手順を都度繰り返す

2. 中断して元に戻す

git rebase --abort

rebase 開始直前の HEAD に戻る。rebase 前のコミットは失われない

3. 状態確認

git status
# rebase in progress; onto <sha>
# Unmerged paths: ...

rebase 中であること、衝突しているファイルがどれかを常に確認する。

4. 事前にコミットを整理する

衝突が多発するなら、git rebase -i HEAD~Nfixup / squash にまとめてから本番の rebase に入ると、衝突回数が減る。

実行例

実際に上記の手順を alpine/git 環境(Git 2.52.0)で動かすと、CONFLICT (content): Merge conflict in f.txt が発生し rebase の終了コードが 1 となった後、マーカー解消・git addgit rebase --continue の一連の操作で終了コード 0 となり Successfully rebased and updated refs/heads/feature. が出力された。

== versions ==
git version 2.52.0
== run ==
--- feature を master に rebase (CONFLICT を想定)---
Rebasing (1/1)
Auto-merging f.txt
CONFLICT (content): Merge conflict in f.txt
error: could not apply a2c42ae... feature: edit line2
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Could not apply a2c42ae... # feature: edit line2
rebase の終了コード: 1
--- git status --short (rebase in progress / Unmerged)---
UU f.txt
--- 解決: feature 側を採用 → add → rebase --continue ---
Updated 1 path from the index
[detached HEAD 8b63b65] feature: edit line2
 1 file changed, 1 insertion(+), 1 deletion(-)
Successfully rebased and updated refs/heads/feature.
continue の終了コード: 0
--- 解決後の f.txt と git log --oneline ---
line1
FEATURE
8b63b65 feature: edit line2
40b9266 main: edit line2
4138e84 init

— 2026-06-09 時点の出力

この記事は役立ちましたか?