1. Overview
1.概述
In Git, there are situations when the branches don’t have a common history base. So, if we try to merge them, we get the “refusing to merge unrelated histories” error. In this tutorial, we’ll discuss how to fix this error and how to avoid this error in future projects.
在Git中,有些情况下分支没有一个共同的历史基础。因此,如果我们试图合并它们,我们会得到“拒绝合并不相关的历史”错误。在本教程中,我们将讨论如何修复这个错误以及如何在未来的项目中避免这个错误。
2. Why Branches Can Have Unrelated Histories?
2.为什么分支机构可以有不相关的历史?
Let’s look at the scenario when the branches have unrelated histories. The most common reason for having an unrelated history base is for starting the branches independently of each other. For example, if we start a new Git project on a local machine and then connect it to a remote GitHub branch, these branches would have different history bases.
让我们来看看当各分支有不相关的历史时的情况。拥有不相关的历史库最常见的原因是为了独立启动各分支。例如,如果我们在本地机器上启动一个新的 Git 项目,然后将其连接到远程的 GitHub 分支,这些分支就会有不同的历史库。
The only exception is when one of the branches has no commits in it. In this case, they should merge without a problem. Otherwise, we’ll get the “refusing to merge unrelated histories” like in the following example:
唯一的例外是当其中一个分支没有提交时。在这种情况下,它们的合并应该没有问题。否则,我们会得到“拒绝合并不相关的历史”,就像下面的例子。
$ git pull origin main
...
fatal: refusing to merge unrelated histories
As we can see, we couldn’t use the git pull command to merge the branches with uncommon history.
我们可以看到,我们无法使用git pull命令来合并具有不常见历史的分支。
3. How to Fix the Error
3.如何修复该错误
To fix the above error, we need to use the option –allow-unrelated-histories after the git pull <remote> <branch> command, where
为了解决上述错误,我们需要在 git pull <remote> <branch> 命令后使用选项-allow-unrelated-histories,其中
- <remote> is the remote repository URL or its short-name origin
- <branch> is the branch name that we’d like to merge
For example:
例如:
$ git pull origin main --allow-unrelated-histories
The –allow-unrelated-histories option will tell the Git that we allow merging branches with no common history base, which then should finish the merge without errors. We should note that for the Git version 2.9 or older, this option is not necessary, and our merge should work without it. To check the Git version, we can use the git –version command.
-allow-unrelated-histories 选项将告诉 Git 我们允许合并没有共同历史基础的分支,这样就可以无误地完成合并。我们应该注意,对于2.9或更早的Git版本,这个选项是不必要的,我们的合并应该在没有它的情况下工作。要检查Git的版本,我们可以使用git -version命令。
4. How to Avoid the Error in the Future
4.如何在未来避免这种错误
Typically, it’s not the best practice to create a local repository branch independently of the remote repository. A more reliable way is to download the remote repository to the local machine using the git clone command, like this:
通常情况下,独立于远程版本库创建本地版本库分支并不是最好的做法。更可靠的方法是使用git clone命令将远程版本库下载到本地机器上,像这样。
$ git clone <repo_url>
This way, we copy the repository from the remote server, and the commit history base remains the same for both remote and local branches.
这样,我们从远程服务器复制版本库,远程和本地分支的提交历史基数都保持不变。
5. Conclusion
5.总结
In this tutorial, we discussed how to merge branches when Git is refusing to merge unrelated histories. We firstly looked at when this error can happen. Then, we used the –allow-unrelated-histories option to fix it. And finally, we learned how to avoid this error in future projects.
在本教程中,我们讨论了当 Git 拒绝合并不相关的历史时如何合并分支。我们首先探讨了这种错误何时会发生。然后,我们使用-allow-unrelated-histories选项来修复它。最后,我们学习了如何在未来的项目中避免这个错误。