MeWrite Docs

CONFLICT (content): Merge conflict in file

Gitでマージ時にコンフリクトが発生した場合の対処法

概要

Merge conflict は、Gitで異なるブランチの変更が同じファイルの同じ箇所を編集している場合に発生します。自動マージができないため、手動での解決が必要です。

エラーメッセージ

Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.

コンフリクトマーカー

<<<<<<< HEAD
現在のブランチの内容
=======
マージしようとしているブランチの内容
>>>>>>> feature-branch

解決方法

1. 手動で編集

1
2
3
4
5
6
7
8
# コンフリクトファイルを開いて編集
# マーカーを削除し、正しい内容を残す

# 解決後、ステージング
git add file.txt

# コミット
git commit

2. どちらかを選択

1
2
3
4
5
6
7
8
# 現在のブランチの内容を採用
git checkout --ours file.txt

# マージ元の内容を採用
git checkout --theirs file.txt

git add file.txt
git commit

3. マージツールを使用

1
2
3
4
5
# 設定されているマージツールを起動
git mergetool

# 特定のツールを指定
git mergetool --tool=vimdiff

4. VS Codeで解決

1
2
3
4
# VS Codeで開く
code file.txt

# エディタ上で「Accept Current」「Accept Incoming」等のボタンを使用

5. マージを中止

1
2
# マージ前の状態に戻る
git merge --abort

事前予防策

1. 頻繁にリベース/マージ

1
2
3
# 作業ブランチを最新に保つ
git fetch origin
git rebase origin/main

2. 小さなコミット

1
2
3
# 大きな変更を避け、小さく頻繁にコミット
git add -p  # 部分的にステージング
git commit -m "小さな変更"

3. Pull時にrebaseを使用

1
git pull --rebase origin main

よくあるシナリオ

package-lock.jsonのコンフリクト

1
2
3
4
# 両方の変更を取り込む
git checkout --theirs package-lock.json
npm install
git add package-lock.json

バイナリファイルのコンフリクト

1
2
3
4
# どちらかを選択するしかない
git checkout --ours image.png
# または
git checkout --theirs image.png

関連エラー

関連エラー

Git の他のエラー

最終更新: 2025-12-17