【Git】merge时报错:refusing to merge unrelated histories

文章目录

一、问题

今天将feature分支合并到master时报错:refusing to merge unrelated histories(拒绝合并无关历史)

报错原因:当尝试从远程仓库"gitee.com:zpg13/system_school"的master分支拉取最新更新并合并到本地的master分支时,Git拒绝了这次合并,原因是两个分支拥有不相关的历史记录。


二、解决办法

1、将feature分支的东西追加到master分支中

这种方法会保留master分支里原本的内容,并且 future的历史记录 会 合并到 master的历史记录中

步骤:

首先切换到master分支

bash 复制代码
git checkout master

然后在执行merge指令的时候添加上: --allow-unrelated-histories 参数

bash 复制代码
git merge feature --allow-unrelated-histories

执行git merge feature --allow-unrelated-histories命令后,可能会出现以下界面:Git要求您提供一个提交消息来解释为什么这次合并是必要的。

请在编辑器中输入您想要的提交消息,然后按Esc键退出编辑模式,再按:wq输入并按Enter键保存并退出Vim编辑器。如果您不想进行任何合并操作,只需按Esc键退出编辑模式,然后在命令行中输入:q并按Enter键退出Vim编辑器。

请注意,如果存在合并冲突,您需要先解决这些冲突,然后再继续合并操作。

此时就会提示合并成功,然后正常推送到远程分支即可


2、将feature里的东西直接覆盖到master分支中

这种方法会丢失master中的所有数据,并且将feature中的历史记录同步到master的历史记录中

先来看看合并前feature的历史记录:

然后再看看合并前master的历史记录:

合并过程:

首先切换到master分支

bash 复制代码
git checkout master

然后使用以下命令进行合并

bash 复制代码
git reset --hard origin/feature

再次查看master分支的日志时可以发现,master中的历史记录已经替换成了feature的历史记录

然后推送的时候使用 -f 参数,强推到远程仓库即可

bash 复制代码
git push origin master -f
相关推荐
全栈陈序员2 小时前
【Git】从入门到精通(十一)—— git reset 三种模式详解
git·elasticsearch
全栈陈序员5 小时前
【Git】从入门到精通(十)—— 撤销与恢复:git checkout与git restore
运维·网络·数据库·git
全栈陈序员12 小时前
【Git】从入门到精通(五)—— git add:将文件加入暂存区
大数据·git·elasticsearch
全栈陈序员15 小时前
【Git】从入门到精通(七)—— git status:查看仓库状态
大数据·git·elasticsearch
莫晓燚20 小时前
Ansible 13 个常用模块---企业实战
git·github
无言听风雨丶1 天前
Kubernetes GitOps 持续交付实战:用 Argo CD 让 Git 成为集群事实源
git·elasticsearch·kubernetes
全栈陈序员1 天前
【Git】从入门到精通(六)—— git commit:提交到版本库
大数据·git·elasticsearch
全栈陈序员2 天前
【Git】从入门到精通(一)—— Git简介:什么是版本控制系统
大数据·git·elasticsearch
大猫会长2 天前
git笔记
git
克里斯蒂亚诺更新2 天前
git引入其他分支并且合并
git