English

Gitのnot concluded your mergeの直し方

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

git merge や git pull で You have not concluded your merge (MERGE_HEAD exists). と出たら、前のマージがまだ終わっていません。git commit(または git merge --continue)でそのマージを終えるか、git merge --abort でやめれば、同じコマンドがそのまま通るようになります。

チームの共有リポジトリから git pull したら競合(コンフリクト)が起き、ファイルを直して git add したあと、コミットせずにもう一度 git pull すると出るメッセージです。検証環境でこのエラーをわざと起こし、出たメッセージと、直したあとに消えるところまでを確かめました。

補足

検証環境は Ubuntu 24.04、Git 2.43.0、bash 5.2 です。英語のメッセージにそろえるため LANG=C で実行しました。リモートには、同じ検証環境の中に作った空のリポジトリを使いました。出力に出るそのパスは /path/to/origin に置き換えています。Windows や macOS、Git の GUI ツールやエディターのマージ機能での表示は試していません。

マージの途中の状態の図。競合して止まり、まだ直していないときは merge や pull で unmerged files のエラーになる。ファイルを直して git add したがコミットしていないときに、You have not concluded your merge が出る。git commit か git merge --continue で終えるか、git merge --abort でやめると抜けられる
競合を直して add したあと、コミットする前に出るエラー

エラーが出るのは、直して add したあと

main に schedule ブランチをマージしたところ、index.html が競合して止まりました。

console
$ git merge schedule
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

index.html を書き直して git add すると、git status に All conflicts fixed but you are still merging. と出ます。競合は直したものの、マージはまだ途中だという意味です。

console
$ git add index.html

$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
	modified:   index.html

ここでコミットせずに、別のブランチ(footer)をマージしようとしたときと、git pull したときのメッセージです。どちらも終了コードは 128 で、何も取り込まずに止まりました。

console
$ git merge footer
fatal: You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you merge.

$ git pull origin main
error: You have not concluded your merge (MERGE_HEAD exists).
hint: Please, commit your changes before merging.
fatal: Exiting because of unfinished merge.

git merge と git pull とで、2行目からの言い方が少し違います。どちらも、前のマージをコミットで終えてから実行するように、と言っています。

git pull で起きた競合でも同じです。検証環境では pull.rebase を設定していないため、手元とリモートの両方に新しいコミットがあるときに --no-rebase を付けずに git pull すると、競合の前に fatal: Need to specify how to reconcile divergent branches. で止まりました。そこで、マージで取り込む指定の --no-rebase を付けて git pull し、競合させました。

直して git add してから、もう一度 git pull すると、上と同じメッセージで止まりました。

console
$ git pull --no-rebase origin main
From /path/to/origin
 * branch            main       -> FETCH_HEAD
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

$ git add index.html

$ git pull --no-rebase origin main
error: You have not concluded your merge (MERGE_HEAD exists).
hint: Please, commit your changes before merging.
fatal: Exiting because of unfinished merge.

MERGE_HEAD は、マージの途中を表すファイル

メッセージの MERGE_HEAD exists は、.git フォルダーに MERGE_HEAD というファイルがある、という意味です。中には、取り込もうとしているブランチ(ここでは schedule)のコミットの ID が入っていました。

console
$ ls .git/MERGE_HEAD
.git/MERGE_HEAD

$ cat .git/MERGE_HEAD
b97dfcf056ccffd465c0fe6bc057719e6d97cb45

$ git rev-parse schedule
b97dfcf056ccffd465c0fe6bc057719e6d97cb45

git merge の公式リファレンスにも、競合して止まったとき、MERGE_HEAD がもう一方のブランチの先頭を指すと書かれています。git merge のソース(builtin/merge.c)もgit pull のソース(builtin/pull.c)も、始める前にこのファイルがあるかを確かめ、あればこのメッセージを出して終えています。

まだ直していないときは、別のメッセージになる

競合したファイルを git add する前に同じことをすると、MERGE_HEAD より先に、直していないファイルがあることを知らせるメッセージになりました。

console
$ git merge footer
error: Merging 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.

$ git pull origin main
error: Pulling 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.

このときは、先に競合したファイルを直して git add します。競合したファイルの直し方は、別の記事「Gitでマージの競合(コンフリクト)を解決する方法」で扱っています。

直し方1:コミットしてマージを終える

直した内容でマージを終えるなら、git merge --continue を実行します。マージコミットができ、MERGE_HEAD はなくなりました。検証では、エディターを開かない設定にして、Git が用意したメッセージのままコミットしています。

console
$ git merge --continue
[main 778bb29] Merge branch 'schedule'

$ ls .git/MERGE_HEAD
ls: cannot access '.git/MERGE_HEAD': No such file or directory

$ git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

このあと、さっき止まった git merge footer と git pull origin main をもう一度実行すると、どちらもエラーは出ませんでした。

console
$ git merge footer
Merge made by the 'ort' strategy.
 footer.html | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 footer.html

$ git pull origin main
From /path/to/origin
 * branch            main       -> FETCH_HEAD
Already up to date.

git merge --continue の代わりに git commit を実行しても同じです。公式リファレンスの、競合の解決のしかたの説明によると、git merge --continue は、マージの途中であることを確かめてから git commit を実行します。git pull で競合したときも、git commit --no-edit で終えたあとは、同じ git pull が通りました。

console
$ git commit --no-edit
[main 7232238] Merge branch 'main' of /path/to/origin

$ git pull --no-rebase origin main
From /path/to/origin
 * branch            main       -> FETCH_HEAD
Already up to date.

--no-edit は、エディターを開かずに、Git が用意したメッセージでコミットするためのオプションです。

注意

git commit -m "メッセージ" index.html のようにファイルを指定してコミットすると、fatal: cannot do a partial commit during a merge. で止まりました。マージを終えるコミットには、ファイル名を付けません。

直し方2:マージをやめて、元に戻す

競合を直してみたものの、いまはマージしないことにしたなら、git merge --abort を実行します。MERGE_HEAD がなくなり、index.html も、直した内容ではなくマージ前の main の内容に戻りました。

console
$ git merge footer
fatal: You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you merge.

$ git merge --abort

$ ls .git/MERGE_HEAD
ls: cannot access '.git/MERGE_HEAD': No such file or directory

$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

$ cat index.html
<h1>ねこノート</h1>
<p>毎週土曜に更新します</p>

$ git log --oneline -1
677e9cb 更新日を土曜にする

$ git merge footer
Merge made by the 'ort' strategy.
 footer.html | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 footer.html

やめたあとは、さっき止まった git merge footer が通りました。公式リファレンスの –abort の説明によると、MERGE_HEAD があるときの git merge --abort は、git reset --merge と同じ動きです。マージを始める前にコミットしていない変更があった場合は、それを元どおりに戻せないことがある、とも書かれています。

マージの途中では、ブランチも移れない

マージを終えないまま git switch でほかのブランチに移ろうとしても、止まりました。

console
$ git switch footer
fatal: cannot switch branch while merging
Consider "git merge --quit" or "git worktree add".

メッセージには git merge --quit が出ています。公式リファレンスによると、--quit は、作業中のファイルとインデックスはそのままにして、マージの途中という状態だけを取りやめるものです。マージを終えたいときは git commit、やめたいときは git merge --abort を使います。

​Posted by 86