Gitでrefusing to merge unrelated historiesエラーの対処
remoteとlocalでcommit履歴が一致していない場合にpullやmergeを行うと以下のエラーが発生する.
refusing to merge unrelated histories
上のリンク先で使用されている画像がわかりやすいので引用するが,こんな感じ.
どういう状況かというと,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.