fatal: refusing to merge unrelated histories

我们进行的 git push, git pullgit merge 操作,有时会遇到这个错误:

$ fatal: refusing to merge unrelated histories

这一般发生于 独立初始化且有 commit 记录的两个仓库第一次 merge 时。 由于两个仓库是独立初始化的,那它们的提交记录确实就是 unrelated histories 的。

找到报错原因就好办, Git 的 pull/push 操作背后都有个 merge 操作,而 merge 操作有一个 --allow-unrelated-histories 参数,参数意思是 允许不相关的额历史。按下面代码所示,在报错的操作上附加这个参数一般就能顺利执行成功啦。

$ git pull --allow-unrelated-histories
$ git push --allow-unrelated-histories
$ git merge --allow-unrelated-histories