Saki's 研究记录

git pull 报错 fatal: 拒绝合并无关的历史

字数统计: 321阅读时长: 1 min
2022/07/28

问题

git仓库中新建了一个仓库,想要把本地的工程目录推送上去:

1
2
3
git init
git add .
git commit "提交说明"

添加远程 git仓库:

1
git remote add origin https://<access_token>@github.com/sakishum/xxx.git

推送到远程仓库:

1
git push origin main

提示错误:

1
2
3
4
5
6
7
git push -u origin main
To https://github.com/sakishum/xxx.git
! [rejected] main -> main (non-fast-forward)
error: 无法推送一些引用到 'https://github.com/sakishum/xxx.git'
提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。
提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
提示:'git push --help' 中的 'Note about fast-forwards' 小节。

按照提示先拉取更新,但提示报错:

1
2
3
4
git pull origin main
来自 https://github.com/sakishum/xxx
* branch main -> FETCH_HEAD
fatal: 拒绝合并无关的历史

解决方法

在拉取分支时使用以下命令:

1
git pull origin main --allow-unrelated-histories

之后在执行:

1
git push origin main

原因

对此问题,官方的解释如下:

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.

以上。

CATALOG
  1. 1. 问题
  2. 2. 解决方法
  3. 3. 原因