Git | fatal: refusing to merge unrelated histories 的解决方案

本文解决在Gitee上新建空仓库后,推送已有仓库时遇到的错误。详细介绍了如何使用命令解决“拒绝合并无关历史”问题,并成功完成推送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景: 在gitee新建了一个空的仓库,推送已存在的仓库。通过remote add origin <git path>指定仓库 ,错误信息:error: failed to push some refs to 'https://gitee.com/XXXX/XXXXX.git' 终端信息:

JerrydeMacBook-Pro:web-h5 jerrylam$ git push      
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

执行了 git push --set-upstream origin master 后又报错,如下 。

JerrydeMacBook-Pro:web-h5 jerrylam$ git push --set-upstream origin master
To https://gitee.com/XXXX/web.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/XXXX/web.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

尝试 git pull origin master, 错误信息如下

JerrydeMacBook-Pro:web-h5 jerrylam$ git pull origin master
From https://gitee.com/XXXX/web.git
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

解决方案

# 将之前的git pull origin master修改为一下代码

git pull origin master --allow-unrelated-histories

终于可以推送了

JerrydeMacBook-Pro:web-h5 jerrylam$ git push origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 3.77 KiB | 3.77 MiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Powered by Gitee.com
To https://gitee.com/XXXX/web.git
   f6b4cf0..c538ad9  master -> master

push 不上去的时候,先 git pull 确保最新代码

git pull  --allow-unrelated-histories
# 或者指定分枝
git pull origin master --allow-unrelated-histories

转载于:https://my.oschina.net/johnsken/blog/1821350

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值