Git refusing to merge unrelated histories on rebas

2018-12-31 20:37发布

During git rebase origin/development the following error message is shown from git:

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

My git version is 2.9.0. Used to work fine in previous version.

How can I continue this rebase allowing unrelated histories with the forced flag introduced in the new release?

标签: git rebase
9条回答
不再属于我。
2楼-- · 2018-12-31 21:19

Try git pull --rebase development

查看更多
皆成旧梦
3楼-- · 2018-12-31 21:19

For this, enter the command as:-

git pull origin branchname --allow-unrelated-histories

Ex:- git pull origin master --allow-unrelated-histories

Reference:- Github unrelated histories issue

查看更多
忆尘夕之涩
4楼-- · 2018-12-31 21:21

In my case, error was just fatal: refusing to merge unrelated histories on every especially first pull request after remotely adding a git repo.

Using --allow-unrelated-histories flag worked with pull request in this way:

git pull origin branchname --allow-unrelated-histories

查看更多
公子世无双
5楼-- · 2018-12-31 21:30

The default behavior has changed since git 2.9:

"git merge" used to allow merging two branches that have no common base by default, which led to a brand new history of an existing project created and then get pulled by an unsuspecting maintainer, which allowed an unnecessary parallel history merged into the existing project. The command has been taught not to allow this by default, with an escape hatch --allow-unrelated-histories option to be used in a rare event that merges histories of two projects that started their lives independently.

See the git release changelog for more information.

You can use --allow-unrelated-histories to force the merge to happen.

查看更多
弹指情弦暗扣
6楼-- · 2018-12-31 21:30

Since all the other answers are not actually answering the question, here is a solution inspired by this answer on a related question.

So you get your error doing git rebase:

$ git rebase origin/development
fatal: refusing to merge unrelated histories
Error redoing merge 1234deadbeef1234deadbeef

This error doesn't actually cancel the rebase, but you are now in the middle of it:

$ git status
interactive rebase in progress; onto 4321beefdead
Last command done (1 command done):
   pick 1234deadbeef1234deadbeef test merge commit

So you can now do the merge by hand. Find out the parent commits of the original merge commit:

$ git log -1 1234deadbeef1234deadbeef
commit 1234deadbeef1234deadbeef
Merge: 111111111 222222222
Author: Hans Dampf
Date:   Wed Jun 6 18:04:35 2018 +0200

    test merge commit

Find out which of the two merge parents is the one that was merged into the current one (probably the second one, verify with git log 222222222), and then do the merge by hand, copying the commit message of the original merge commit:

$ git merge --allow-unrelated 222222222 --no-commit
Automatic merge went well; stopped before committing as requested
$ git commit -C 1234deadbeef1234deadbeef
[detached HEAD 909af09ec] test merge commit
 Date: Wed Jun 6 18:04:35 2018 +0200
$ git rebase --continue
Successfully rebased and updated refs/heads/test-branch.
查看更多
低头抚发
7楼-- · 2018-12-31 21:32

I got this error when I set up a local repository first. Then went to github and created a new repository. Then I ran

git remote add origin <repository url>

When I tried to push/pull, I got the same fatal: unrelated_histories error. Here is how I fixed it:

git pull origin master --allow-unrelated-histories
git merge origin origin/master
... add and commit here...
git push origin master
查看更多
登录 后发表回答