在git重新启动/开发过程中,git显示以下错误消息:

fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef

我的Git版本是2.9.0。它在以前的版本中运行良好。

我如何才能继续使用新版本中引入的强制标志来允许不相关的历史?


当前回答

我也有同样的问题。试试看:

git pull origin master --allow-unrelated-histories 

git push origin master

其他回答

我也犯了同样的错误,这个命令对我有效:

git pull gitlab master --allow-unrelated-histories

请注意,在您的案例中,gitlab可能是origin或heroku。

我也有同样的问题。问题很遥远,有一些东西阻止了这一点。

我首先创建了一个本地存储库。我将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 2.9以来,默认行为发生了变化:

“gitmerge”用于合并两个没有公共项的分支默认情况下,这导致了现有项目被创建,然后被一个不知情的维护人员拉住,这允许将不必要的并行历史合并到现有项目。命令已被教导不允许这样做默认情况下,带有转义线--允许不相关的历史记录选项用于合并两个项目历史的罕见事件独立开始他们的生活。

有关更多信息,请参阅Git发布变更日志。

更多信息可以在这个答案中找到。

我认为这是一种非常罕见的情况;您已删除远程(例如,原点):

git remote rm origin

然后,过了一段时间,出于某种原因,您尝试将其添加回去,但输入了错误的URL。

git remote add origin git@github.com:example/wrong.git

当然,解决方案是使用正确的URL:

git remote add origin git@github.com:example/correct.git

如果您在GitHub上创建了一个新的存储库,并意外地用README或.gitignore文件初始化了它。

如果您发现由于.git文件夹损坏而无法合并或重新创建基。

然后:

创建新文件夹git克隆将所有文件粘贴到此文件夹

现在,本地和远程将拥有“相关历史”,并将愉快地合并或重新基础。