状況
ローカルで作成したリポジトリにgit add remote
し、pullしようとしたらrefusing to merge unrelated histories
になった。
1 | $ git remote add origin https://github.com/shosatojp/hoge.git |
pushするとpullしろと言われる。ここまではいい。
これが今回gitに怒られたところ
1 | $ git pull origin master |
「refusing to merge unrelated histories」のエラーが発生している。
Refusing to Merge Unrelated Historiesとは何か
gitの公式サイトにはmergeのオプションでこのように書かれている
By default, git merge command refuses to merge histories that do not share a common ancestor. This option can be used to override this safety when merging histories of two projects that started their lives independently.
https://git-scm.com/docs/git-merge#Documentation/git-merge.txt---allow-unrelated-histories
つまり、
デフォルトでは、共通の祖先を持たないブランチ同士のmergeは安全のために無効にされているというのだ。
今回のケースではgithubでリポジトリを作りreadme.mdを追加し、ローカルでファイルを追加してから、git remote add
をしたため共通の祖先を持たないブランチ同士ということになったものと思われる。
結論
merge
コマンドで祖先を共有しないブランチ同士のmergeを許可する--allow-unrelated-histories
オプションを使用すればよい。このオプションはpull
でも使える
1 | $ git pull --allow-unrelated-histories origin master |