shuhelohelo’s blog

Xamarin.Forms多めです.

Gitでrefusing to merge unrelated historiesエラーの対処

www.educative.io

remoteとlocalでcommit履歴が一致していない場合にpullやmergeを行うと以下のエラーが発生する.

refusing to merge unrelated histories

上のリンク先で使用されている画像がわかりやすいので引用するが,こんな感じ.

f:id:shuhelohelo:20200826144230p:plain

どういう状況かというと,remoteとlocalで別々に作成されたリポジトリがあって,local側でremote側のリポジトリgit remote addコマンドで追加した,という状況.

local側でfb62efcのコミットが行われ,それとは別にremote側でda7cc7aのコミットが行われている.

このため,local側とremote側で保持しているコミット履歴が異なるためにエラーが発生している,という状況.

このエラーが発生している状態でgit logを実行すると,以下のとおり.

> git log --oneline --all
da7cc7a (origin/master) Initial commit
fb62efc (HEAD -> master) Created sln and added gitignore file.

これに対して以下のコマンドを実行する.

git pull origin master --allow-unrelated-histories
> git pull origin master --allow-unrelated-histories
From https://github.com/shuheydev/xUnitPractice
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

コンフリクトなどがなければこれまで関連のない2つのリポジトリがマージされて以下のようになる.

> git log --all --oneline --graph
*   df81915 (HEAD -> master) Merge branch 'master' of https://github.com/shuheydev/xUnitPractice into master
|\
| * da7cc7a (origin/master) Initial commit
* fb62efc Created sln and added gitignore file.