できない.dev

Git merge でコンフリクトが起きてマージを完了できない

git merge でコンフリクトが起きたら、衝突マーカーを直して git add し、git commit すればマージが完了する。
やめたいときは git merge --abort で開始前に戻せる。

公開: 更新:

症状できない · exit 1
git merge feature
Auto-merging f.txt
CONFLICT (content): Merge conflict in f.txt
Automatic merge failed; fix conflicts and then commit the result.
解決後できた · exit 0
git commit -m "resolve conflict (take feature)"
[master 5cadcce] resolve conflict (take feature)

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

要約

git merge 中に表示される CONFLICT (content): は、両方のブランチが同じ箇所を変更したため、自動マージできなかったことを示す。
衝突マーカーを編集して git add し、最後に git commit を打てば merge は完了する。
途中でやめたい場合は、git merge --abort で開始前の状態に戻せる。

実行例

alpine/git 環境(git 2.52.0)で上記の手順を動かすと、git merge feature は CONFLICT (content) を出して終了コード 1 で止まった。
git checkout --theirs で片側を採用して git commit すると、終了コード 0 で merge が完了した。

$ git merge feature
Auto-merging f.txt
CONFLICT (content): Merge conflict in f.txt
Automatic merge failed; fix conflicts and then commit the result.
$ echo $?
1
$ git status --short
UU f.txt
$ git checkout --theirs f.txt
Updated 1 path from the index
$ git add f.txt
$ git commit -m "resolve conflict (take feature)"
[master 5cadcce] resolve conflict (take feature)
$ echo $?
0
$ git log --oneline
5cadcce resolve conflict (take feature)
7c06bac main
62610e5 feature
96321f7 init

— 2026-08-02 時点の出力

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

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

解決策

1. 標準的な解消フロー

git merge feature
# CONFLICT が出たら
$EDITOR path/to/file        # マーカーを消し、正しい内容に編集
git add path/to/file
git commit                  # 既定のメッセージのまま OK

git status で「Unmerged paths」の一覧を見ながら、1 ファイルずつ片付ける。
衝突の表示のされ方は、git-merge 公式ドキュメント(新しいタブで開く) の「HOW CONFLICTS ARE PRESENTED」に詳しい。

2. 中断して元に戻す

git merge --abort

merge 開始直前の HEAD に戻る。
未コミットのローカル変更は失われない。
手詰まりになったら、まずこれで初期状態に戻してからやり直す。

3. 状態確認

git status
# You have unmerged paths.
git diff --name-only --diff-filter=U

衝突しているファイルだけを抽出できる。
merge 中は、HEAD と MERGE_HEAD の 2 つのコミットが指される。

4. バイナリ衝突は片側採用

git checkout --ours  path/to/image.png   # 自分側を採用
git checkout --theirs path/to/image.png  # 相手側を採用
git add path/to/image.png

テキストエディタで開けないファイルは、どちらかを丸ごと採用する。
--ours と --theirs が意味を持つのは merge 中だけである。

よくある原因

  1. マーカーが残っている。
    <<<<<<< ======= >>>>>>> を消し忘れたまま git add すると、ビルドが壊れたコードがコミットされる。
  2. commit を打ち忘れている。
    衝突を解消したが git commit を打たず、merge in progress のまま放置している。
  3. abort を知らない。
    解消が難しくて行き詰まると、リポジトリを clone し直すしかないと考えてしまう。
  4. バイナリが衝突している。
    画像や PDF など、差分でマージできないファイルが衝突している。