在git重新启动/开发过程中,git显示以下错误消息:
fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef
我的Git版本是2.9.0。它在以前的版本中运行良好。
我如何才能继续使用新版本中引入的强制标志来允许不相关的历史?
在git重新启动/开发过程中,git显示以下错误消息:
fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef
我的Git版本是2.9.0。它在以前的版本中运行良好。
我如何才能继续使用新版本中引入的强制标志来允许不相关的历史?
当前回答
我也有同样的问题。问题很遥远,有一些东西阻止了这一点。
我首先创建了一个本地存储库。我将LICENSE和README.md文件添加到本地并提交。
然后我想要一个远程存储库,所以我在GitHub上创建了一个。在这里,我错误地选中了“用README初始化这个存储库”,这也在远程创建了一个README.md。
所以现在当我跑步时
git push --set-upstream origin master
我得到了:
error: failed to push some refs to 'https://github.com/lokeshub/myTODs.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes
(e.g. hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
现在为了克服这一点,我做到了
git pull origin master
导致以下错误:
From https://github.com/lokeshub/myTODs
branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories**
我尝试了:
git pull origin master --allow-unrelated-histories
结果:
From https://github.com/lokeshub/myTODs
* branch master -> FETCH_HEAD
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed;
fix conflicts and then commit the result.
解决方案:
我删除了远程存储库,并创建了一个新的(我认为只有删除文件README才能起作用),之后,以下操作奏效:
git remote rm origin
git remote add origin https://github.com/lokeshub/myTODOs.git
git push --set-upstream origin master
其他回答
如果您有存储库的浅Git克隆,也会发生这种情况。
我最近在CI/CD设置中遇到了这个问题,以前的解决方案都不适合我。我正在构建一个CI/CD管道,以分析MR源分支的MR创建代码。为此,我需要在main分支上运行一次分析,然后在与main合并的MR源分支上运行分析,在尝试通过gitmerge命令合并分支时出现了这个错误。
CI/CD设置中出现这种情况的原因:通常在CI/CD环境中,Git存储库是一个用来加快速度的浅层克隆,它不包括完整的提交历史,因此,在合并Git时,我们可能会认为我们正在尝试合并不相关的分支,这实际上是不正确的。
解决方案:
使用以下命令将浅存储库转换为非浅存储库:
git fetch --unshallow
尝试git pull--重新基础开发
警告:这可能会覆盖远程存储库
这对我有用:
git push origin master --force
我也很挣扎,但我设法找到了解决办法。
当您遇到上面的错误时,只需选择合并提交,然后继续重新启动:
git cherry-pick -m 1 1234deadbeef1234deadbeef
git rebase --continue
在我的案例中,错误是致命的:每次尝试都拒绝合并不相关的历史记录,尤其是远程添加Git存储库后的第一次拉取请求。
使用--allow不相关的历史标记以这种方式处理拉取请求:
git pull origin branchname --allow-unrelated-histories
根据2.9.0发行说明,git pull已被教导将--allow unrelated history选项传递给基础git merge