English

GitのCONFLICT (modify/delete)の直し方

​​Git​コマンドライン,トラブルシュート

git merge で CONFLICT (modify/delete) と出て止まるのは、片方のブランチで消したファイルを、もう片方のブランチで書き直したときです。ファイルを残すなら git add、消すなら git rm でどちらにするかを決め、git commit でマージを終えれば、競合(コンフリクト)は解消します。

いらなくなったページを削除したブランチに、同じページを直した別のブランチをマージしたときや、直した側のブランチを、消した側の上に git rebase で積み直したときに出るメッセージです。検証環境でわざと起こし、出たメッセージと、直したあとに消えるところまでを確かめました。

補足

検証環境は Ubuntu 24.04、Git 2.43.0、bash 5.2 です。英語のメッセージにそろえるため LANG=C で実行しました。リポジトリは手元に作ったものだけで、リモートとのやり取り(git pull で起きた競合)、git mergetool やエディター・GUI ツールでの表示、Windows・macOS は試していません。

消した側と直した側がぶつかると止まる

main ブランチでは news.html を消し、update-news ブランチでは同じ news.html の文章を書き直しました。

console
$ git log --oneline --all --stat
fc07865 お知らせのページを消す
 news.html | 2 --
 1 file changed, 2 deletions(-)
0fda084 お知らせを書き直す
 news.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
b976f85 最初のコミット
 index.html | 1 +
 news.html  | 2 ++
 2 files changed, 3 insertions(+)

main に update-news をマージすると、次のメッセージで止まりました。終了コードは 1 です。

console
$ git merge update-news
CONFLICT (modify/delete): news.html deleted in HEAD and modified in update-news.  Version update-news of news.html left in tree.
Automatic merge failed; fix conflicts and then commit the result.

deleted in HEAD は「いまいるブランチ(main)で消した」、modified in update-news は「update-news で書き直した」という意味です。Version update-news of news.html left in tree. のとおり、作業中のフォルダには、書き直した側の news.html が置かれていました。

console
$ cat news.html
<h1>お知らせ</h1>
<p>12月は毎週土曜に更新します</p>

Git は「消す」と「直す」のどちらも選ばずに止まり、どちらにするかを人に任せます。ふつうの競合と違い、ファイルの中に <<<<<<< などの競合マーカーは入っていませんでした。

CONFLICT (modify/delete) の直し方の図。main で news.html を消し、update-news で news.html を直してマージすると、直した側のファイルが残った状態で止まる。直した内容を残すなら git add news.html、消すなら git rm news.html を実行し、git commit でマージを終える
消した側と直した側のどちらを取るかを選んで、コミットする

git status で、どちらが消したのかを見る

git status では、Unmerged paths に deleted by us と出ました。us(自分)は、いまいるブランチのことです。

console
$ git status
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
	deleted by us:   news.html

no changes added to commit (use "git add" and/or "git commit -a")

短い表示(git status --short)では DU です。git status の公式リファレンスの表で、DU は「unmerged, deleted by us」、逆の UD は「unmerged, deleted by them」となっています。

console
$ git status --short
DU news.html

反対に、書き直した update-news にいて、消した main をマージしたときは、メッセージの HEAD の位置が入れ替わり、deleted by them になりました。作業中のフォルダに残るのは、このときも書き直した側(ここでは HEAD)の news.html です。

console
$ git merge main
CONFLICT (modify/delete): news.html deleted in main and modified in HEAD.  Version HEAD of news.html left in tree.
Automatic merge failed; fix conflicts and then commit the result.

$ git status
On branch update-news
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
	deleted by them: news.html

no changes added to commit (use "git add" and/or "git commit -a")

消す前に、相手が何を直したかを確かめる

消すか残すかを決める前に、直した側がどこを変えたのかを見ておきます。競合しているあいだ、git ls-files --stage には、同じファイルが番号つきで並びます。

console
$ git ls-files --stage news.html
100644 f885a57669fca24ef11e414030ab06ebe018778c 1	news.html
100644 4a8eb34da990a3c20c2fc596d532b5087220d9f8 3	news.html

リビジョンの指定方法の公式リファレンスによると、マージ中の 1 は共通の祖先、2 はいまいるブランチ、3 はマージしようとしているブランチの内容です。ここでは main で消したので 2 がありません。1 と 3 を比べると、update-news で書き直した行だけが出ました。

console
$ git diff :1:news.html :3:news.html
diff --git a/news.html b/news.html
index f885a57..4a8eb34 100644
--- a/news.html
+++ b/news.html
@@ -1,2 +1,2 @@
 <h1>お知らせ</h1>
-<p>12月の更新はお休みです</p>
+<p>12月は毎週土曜に更新します</p>

deleted by them のときは、残っているのが 1 と 2 なので、git diff :1:news.html :2:news.html で自分の直した行を確かめます。

直し方1:直した内容を残すなら git add

ファイルを残すと決めたら、git add してからコミットします。git status が All conflicts fixed but you are still merging. に変わり、コミットされる変更に news.html が new file として並びました。

console
$ git add news.html

$ git status
On branch main
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
	new file:   news.html

$ git commit --no-edit
[main f59953b] Merge branch 'update-news'

$ git status
On branch main
nothing to commit, working tree clean

--no-edit は、エディターを開かずに、Git が用意したメッセージのままコミットするオプションです。コミットのあとは、競合しているファイルを一覧にする git diff --name-only --diff-filter=U も何も出さなくなり、news.html は書き直した内容で残りました。

console
$ git diff --name-only --diff-filter=U

$ git ls-files
index.html
news.html

$ cat news.html
<h1>お知らせ</h1>
<p>12月は毎週土曜に更新します</p>

直し方2:消すほうを選ぶなら git rm

ファイルを消すと決めたら、git rm してからコミットします。同じ状態からやり直したところ、news.html はフォルダからもリポジトリからもなくなりました。

console
$ git rm news.html
rm 'news.html'

$ git status
On branch main
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)


$ git commit --no-edit
[main 441e752] Merge branch 'update-news'

$ git status
On branch main
nothing to commit, working tree clean

$ ls
index.html

update-news にいて deleted by them になったときも、git rm news.html と git commit --no-edit で同じように終えられ、working tree clean になりました。

注意

git rm を選ぶと、相手のブランチで直した内容も一緒に消えます。直した文章を別のファイルに移したいときは、git rm の前に cat news.html などで内容を控えておきます。

git checkout –ours では選べない

ふつうの競合では、git checkout の公式リファレンスにあるとおり、git checkout --ours や --theirs で片方の内容をまるごと使えます。modify/delete の競合で、消した側を指定すると、次のエラーになりました。

console
$ git checkout --ours news.html
error: path 'news.html' does not have our version

deleted by them のときに --theirs を指定しても、error: path 'news.html' does not have their version でした。消した側にはファイルの中身がないので、取り出せません。消す場合は git rm を使います。

また、どちらも選ばないまま git commit すると、止まりました。

console
$ git commit --no-edit
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
U	news.html

マージそのものをやめたいときは、git merge --abort でマージ前の状態に戻せます。検証では、news.html のない main の状態に戻り、working tree clean になりました。

git rebase では、HEAD は積み直し先の main になる

update-news を main の上に git rebase したときも、同じ CONFLICT (modify/delete) で止まりました。

console
$ git rebase main
Rebasing (1/1)CONFLICT (modify/delete): news.html deleted in HEAD and modified in 0fda084 (お知らせを書き直す).  Version 0fda084 (お知らせを書き直す) of news.html left in tree.
error: could not apply 0fda084... お知らせを書き直す
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".
Could not apply 0fda084... お知らせを書き直す

自分は update-news で作業していたのに、メッセージは deleted in HEAD、git status も deleted by us でした。rebase では、取り込む先の main の上に自分のコミットを1つずつ積み直すので、HEAD(us)は main 側になります。git rebase の公式リファレンスにも、rebase では ours と theirs が入れ替わると書かれています。

書き直した内容を残すなら、マージと同じく git add をしてから、git commit の代わりに git rebase --continue を実行します。rebase が終わり、working tree clean になりました。

console
$ git add news.html

$ git rebase --continue
[detached HEAD 1a16d99] お知らせを書き直す
 1 file changed, 2 insertions(+)
 create mode 100644 news.html
Successfully rebased and updated refs/heads/update-news.

$ git status
On branch update-news
nothing to commit, working tree clean

ファイルの中に競合マーカーが入る、ふつうの競合の直し方は、別の記事「Gitでマージの競合(コンフリクト)を解決する方法」で扱っています。

​Posted by 86