English

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

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

git merge で CONFLICT (rename/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 ブランチでは about.html を消し、rename-page ブランチでは git mv で about.html を profile.html に名前を変えました。

console
$ git log --oneline --all --stat
6024ad0 about.html を消す
 about.html | 5 -----
 1 file changed, 5 deletions(-)
1d0e66a about.html を profile.html に名前を変える
 about.html => profile.html | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
3553433 最初のコミット
 about.html | 5 +++++
 index.html | 1 +
 2 files changed, 6 insertions(+)

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

console
$ git merge rename-page
CONFLICT (rename/delete): about.html renamed to profile.html in rename-page, but deleted in HEAD.
Automatic merge failed; fix conflicts and then commit the result.

about.html renamed to profile.html in rename-page は「rename-page で about.html の名前を profile.html に変えた」、but deleted in HEAD は「いまいるブランチ(main)では消した」という意味です。

作業中のフォルダには、新しい名前の profile.html が置かれていました。古い名前の about.html はありません。ファイルの中に <<<<<<< などの競合マーカーは入っていませんでした。

console
$ ls
index.html
profile.html

$ cat profile.html
<h1>このブログについて</h1>
<p>猫と暮らす毎日を書いています</p>
<p>写真は自分で撮っています</p>
<p>コメントは承認してから表示します</p>
<p>連絡はお問い合わせページからどうぞ</p>
CONFLICT (rename/delete) の直し方の図。main で about.html を消し、rename-page で about.html の名前を profile.html に変えてマージすると、新しい名前の profile.html が残った状態で止まる。新しい名前で残すなら git add profile.html、消すなら git rm profile.html を実行し、git commit でマージを終える
名前を変えた側と消した側のどちらを取るかを選んで、コミットする

git status には新しい名前で出る

git status では、Unmerged paths に、古い名前ではなく新しい名前の profile.html が 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:   profile.html

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

$ git status --short
DU profile.html

git status の公式リファレンスの表で、DU は「unmerged, deleted by us」、逆の UD は「unmerged, deleted by them」です。git add や git rm に渡すファイル名も、ここに出ている profile.html です。

git ls-files --stage で見ると、競合しているのは profile.html だけで、ステージ番号 1 と 3 の2行が並びました。

console
$ git ls-files --stage
100644 e54fb8dd943105190f8616b525110e6fb8853eec 0	index.html
100644 d42016180abc251cae587bc62c5aee210558db5b 1	profile.html
100644 d42016180abc251cae587bc62c5aee210558db5b 3	profile.html

リビジョンの指定方法の公式リファレンスによると、マージ中の 1 は共通の祖先、2 はいまいるブランチ、3 はマージしようとしているブランチの内容です。共通の祖先にあった about.html の内容は、新しい名前の profile.html としてステージ 1 に入っていました。main で消したので 2 はありません。

直し方1:新しい名前で残すなら git add

名前を変えたファイルを残すと決めたら、git add してからコミットします。

console
$ git add profile.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:   profile.html

$ git commit --no-edit
[main e9590fd] Merge branch 'rename-page'

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

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

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

$ git ls-files
index.html
profile.html

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

ファイルを消すと決めたら、新しい名前の profile.html を git rm してからコミットします。同じ状態からやり直したところ、古い名前も新しい名前も、フォルダとリポジトリの両方からなくなりました。

console
$ git rm profile.html
rm 'profile.html'

$ git commit --no-edit
[main 79c12a5] Merge branch 'rename-page'

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

$ git ls-files
index.html

マージそのものをやめたいときは git merge --abort を実行します。検証では、about.html も profile.html もない、マージ前の main に戻りました。

名前を変えた側でマージすると deleted by them になる

名前を変えた rename-page にいて、消した main をマージすると、メッセージは renamed to profile.html in HEAD となり、名前を変えた側が HEAD として示されました。

console
$ git merge main
CONFLICT (rename/delete): about.html renamed to profile.html in HEAD, but deleted in main.
Automatic merge failed; fix conflicts and then commit the result.

$ git status --short
UD profile.html

git status では deleted by them: profile.html です。このときは、ファイルの中身があるのは自分の側だけなので、git checkout --ours では新しい名前の profile.html を取り出せました。--theirs はエラーになりました。

console
$ git checkout --theirs profile.html
error: path 'profile.html' does not have their version

$ git checkout --ours profile.html
Updated 1 path from the index

main にいて deleted by us のときは、反対に --ours が error: path 'profile.html' does not have our version になりました。git checkout の –ours や –theirs は片方の中身を取り出すもので、消す側は選べません。消す場合は、どちらの向きでも新しい名前で git rm profile.html とします。rename-page にいるときも、git rm と git commit --no-edit で working tree clean になりました。

名前の変更と同時に中身も直していたとき

rename-page で名前を変えたあと、profile.html の1行を書き直してからマージすると、CONFLICT が2行出ました。

console
$ git merge rename-page
CONFLICT (rename/delete): about.html renamed to profile.html in rename-page, but deleted in HEAD.
CONFLICT (modify/delete): profile.html deleted in HEAD and modified in rename-page.  Version rename-page of profile.html left in tree.
Automatic merge failed; fix conflicts and then commit the result.

競合しているのは、このときも profile.html の1つだけで、git status --short は DU profile.html でした。名前を変えた側で書き直した行は、共通の祖先(1)と相手側(3)を比べると分かります。

console
$ git diff :1:profile.html :3:profile.html
diff --git a/profile.html b/profile.html
index d420161..0e6118a 100644
--- a/profile.html
+++ b/profile.html
@@ -1,5 +1,5 @@
 <h1>このブログについて</h1>
-<p>猫と暮らす毎日を書いています</p>
+<p>猫と暮らす毎日を、週に1回書いています</p>
 <p>写真は自分で撮っています</p>
 <p>コメントは承認してから表示します</p>
 <p>連絡はお問い合わせページからどうぞ</p>

直し方は、名前を変えただけのときと同じでした。同じ状態から、git add profile.html で残す場合と、git rm profile.html で消す場合の両方を試しました。どちらも git commit --no-edit のあとに working tree clean になり、git diff --name-only --diff-filter=U も何も出さなくなりました。2行目の modify/delete そのものは、別の記事「GitのCONFLICT (modify/delete)の直し方」で扱っています。

中身を大きく書き換えると、競合にならずにマージされる

名前を変えたあとに中身をほとんど書き換えた場合は、競合が起きませんでした。5行あった about.html を、2行のまったく違う profile.html にしたところ、Git は名前の変更ではなく「about.html を消して、profile.html を新しく作った」と見なしました。

console
$ git diff --stat -M HEAD~1 rename-page
 about.html   | 5 -----
 profile.html | 2 ++
 2 files changed, 2 insertions(+), 5 deletions(-)

$ git merge rename-page
Merge made by the 'ort' strategy.
 profile.html | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 profile.html

git merge の公式リファレンスによると、マージでは名前の変更を見つける処理が最初から有効で、どれだけ似ていれば名前の変更と見なすかは git diff の –find-renames と同じしくみです。名前の変更と見なす類似度の既定は 50% です。

注意

名前の変更と見なされないと、main で消したはずのページが、新しい名前でそのまま入ってきます。競合のメッセージは出ないので、消した側のブランチにマージしたあとは、git ls-files などで、いらないファイルが増えていないかを確かめます。

git rebase では、名前を変えた側がコミットで示される

rename-page を main の上に git rebase したときも、同じ CONFLICT (rename/delete) で止まりました。マージと違い、名前を変えた側はブランチ名ではなく、積み直しているコミット(1d0e66a)で示されます。

console
$ git rebase main
Rebasing (1/1)CONFLICT (rename/delete): about.html renamed to profile.html in 1d0e66a (about.html を profile.html に名前を変える), but deleted in HEAD.
error: could not apply 1d0e66a... about.html を profile.html に名前を変える
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 1d0e66a... about.html を profile.html に名前を変える

rename-page で作業しているのに deleted in HEAD、git status も deleted by us: profile.html になるのは、rebase では ours と theirs が入れ替わるためです(git rebase の公式リファレンス)。ファイル名は、このときも新しい名前の profile.html でした。新しい名前で残すなら、git add profile.html のあとに git rebase --continue を実行します。

console
$ git add profile.html

$ git rebase --continue
[detached HEAD dd9310d] about.html を profile.html に名前を変える
 1 file changed, 5 insertions(+)
 create mode 100644 profile.html
Successfully rebased and updated refs/heads/rename-page.

$ git status
On branch rename-page
nothing to commit, working tree clean

​Posted by 86