Git 拒绝合并不相关的历史。什么是“不相关的历史”?

在本地,我创建了一个新的文本文件→ git add newfile.txt → commit → pull source master → ERROR!

“拒绝合并不相关的历史”。

什么是不相关的历史?

什么是相关的历史?

109575 次浏览

I think you have committed in the remote repository, and when you pull, this error happens.

Use this command:

git pull origin master --allow-unrelated-histories
git merge origin origin/master

When somehow the local .git subdirectory is lost, the whole project seems to appear from nowhere, as all the local changes histories were contained by .git. Thus, your local changes become unrelated. That is why all the changes are called unrelated histories then.

In this situation, git merge or pull request will unable to track where you made changes to add with the remote project. Hence, " refusing to merge unrelated histories"- error occurs.

In this situation, if you try to force merge by following commands,

git pull origin master --allow-unrelated-histories

git merge origin origin/master

It will create a lot of conflicts, as it is not able to find the history of your local changes.

I got this issue when renaming one of the repositories in GitHub Enterprise, and then making the same former named repository as the below steps.

  1. create repository sample in remote
  2. clone it to local
  3. rename remote to old-sample
  4. create a new repository named sample in remote
  5. try to push it from local
  6. error occurred.

The weird thing is that I removed the local repository, but git tries to clone old one even if I try to clone by newly created repository URL. The clone URL is just automatically redirected to the old one, even if the new one exists in remote. I don't know if this is due to the server or local Git process.

For this reason, the error occurs because the Git process fails to compare its local commit history to the remote history.

The unrelated histories could tell in the above situation.

I ended up removing and renamed the old repository in remote and it was solved.

I ran into a similar problem where I brought in a branch from a second remote and wanted to merge with a branch from the first remote. This is different from the existing answers as I'm not using --allow-unrelated-histories on the pull, but on the merge.

git checkout master
git merge --allow-unrelated-histories myfunnybrancy
git pull origin master --allow-unrelated-histories