できない.dev

Git cherry-pick でコンフリクトが起きて続行できない

git cherry-pick でコンフリクトしたら、マーカーを直して git add し --continue で続行する。
やめるなら git cherry-pick --abort で開始前に戻せる。
merge と同じ手順で片付く。

公開: 更新:

症状できない · exit 1
git cherry-pick feature
…
error: could not apply 67a3b15... feature: edit line2
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
解決後できた · exit 0
git cherry-pick --continue
[master 6869f43] feature: edit line2
 Date: Sun Aug 2 08:42:36 2026 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)

2026-08-02 に alpine/git で実際に打って取った出力。検証環境の詳細

要約

git cherry-pick のコンフリクトも、基本は merge / rebase と同じ流れで解消する。
<<<<<<< マーカーを潰したら、git add → git cherry-pick --continue の順に打つ。
やめたいときは git cherry-pick --abort で開始前の HEAD に戻る。
merge コミットを cherry-pick する場合だけ、-m で親番号を指定する。

実行例

alpine/git(Git 2.54.0)環境で上記の手順を動かすと、git cherry-pick は CONFLICT を検出し、終了コード 1 で停止した。
マーカーを解消して git add → git cherry-pick --continue と進めると、終了コード 0 でコミットが正常に適用された。

$ git cherry-pick feature
Auto-merging f.txt
CONFLICT (content): Merge conflict in f.txt
error: could not apply 67a3b15... feature: edit line2
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
$ echo $?
1
$ git status --short
UU f.txt
$ git checkout --theirs f.txt
Updated 1 path from the index
$ git add f.txt
$ git cherry-pick --continue
[master 6869f43] feature: edit line2
 Date: Sun Aug 2 08:42:36 2026 +0000
 1 file changed, 1 insertion(+), 1 deletion(-)
$ echo $?
0
$ cat f.txt
line1
FEATURE
$ git log --oneline
6869f43 feature: edit line2
6945f3b main: edit line2
5ba8aa1 init

— 2026-08-02 時点の出力

検証環境2026-08-02 検証
検証日
実行環境
alpine/git

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. 衝突を解消して続行

git cherry-pick <sha>
# CONFLICT が出たら
$EDITOR path/to/file        # マーカーを消し正しい内容にする
git add path/to/file
git cherry-pick --continue

git status で cherry-picking <sha> と衝突ファイルが確認できる。

2. 中止して元に戻す

git cherry-pick --abort

cherry-pick 開始前の HEAD に戻る。
--skip は当該コミットを飛ばして次に進む。

3. 範囲指定の cherry-pick

連続コミットをまとめて当てたい場合は、範囲で指定する。

git cherry-pick A^..C

A^..C は A を含み、A..C は含まない。
途中で衝突したら、同じく --continue で進める(公式ドキュメント(新しいタブで開く))。

4. merge コミットを cherry-pick

git cherry-pick -m 1 <merge-sha>

-m 1 で「親 1 番からの差分を当てる」と明示する。
どちらの親が正しいかはマージ元のブランチに合わせる。

よくある原因

  1. 同じ行を別ブランチで既に変更している。
    cherry-pick はコミットの差分を再適用するため、当ブランチ側で同じ箇所が変わっていれば必ず衝突する。
  2. continue を忘れて commit した。
    通常どおり git commit してしまうと、cherry-pick の進行管理が壊れる。
  3. マーカーを残したまま add した。
    <<<<<<< HEAD / ======= / >>>>>>> を消し忘れると、壊れた内容を git add してしまう。
  4. merge コミットを直接 cherry-pick した。
    親が複数あるコミットには -m で親番号の指定が要り、指定なしだと is a merge but no -m option was given で止まる。