Git fatal: refusing to merge unrelated histories问题处理

36 篇文章 4 订阅

情况介绍

我一开始在本地创建了仓库git init,有了一些提交。然后后面准备上传到github时,在github上创建了仓库,选了License,也就是说远程仓库也有了一个提交(即生成License的提交)。

在本地添加remote后,git push是不成功的,因为远程和本地不一致,也没有关联。所以据需要先pull,然后合并一下才能提交到远程仓库。

然后pull的时候报了个错,如下:

$ git pull origin main
From github.com:user/repo
 * branch            main       -> FETCH_HEAD
fatal: refusing to merge unrelated histories

按照字面意思就是:拒绝合并没有关联的历史。

排查

我们看看文档

$ git pull --help

这个命令会在浏览器打开本地的git文档,,比如我的是:file:///H:/Git/mingw64/share/doc/git-doc/git-pull.html,搜一下:unrelated,找到了下面的内容:

--allow-unrelated-histories
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. As that is a very rare occasion, no configuration variable to enable this by default exists and will not be added.

翻译过来是:

默认情况下,git
merge命令拒绝合并没有共同祖先的历史记录。此选项可用于在合并独立启动的两个项目的历史记录时覆盖此安全性。由于这种情况非常罕见,因此默认情况下不存在启用该功能的配置变量,也不会添加。

在这里,因为本地和远程内容都比较简单,且明确没有冲突,就是要合并的。那就添加这个参数,强制让它执行合并就行了。(注意:如果有冲突就要谨慎操作,同时以防万一,操作前先备份!!!)

$ git pull origin main --allow-unrelated-histories
From github.com:user/repo
 * branch            main       -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 LICENSE | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100644 LICENSE

$ git logg
*   b06c151 (HEAD -> main) Merge branch 'main' of github.com:user/repo
|\
| * 9e6ef31 (origin/main) Initial commit

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值