You can use --allow-unrelated-histories to force the merge to happen.
The reason behind this is that 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-historiesoption 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.
More information can be found in this answer.
Answer from blue112 on Stack OverflowYou can use --allow-unrelated-histories to force the merge to happen.
The reason behind this is that 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-historiesoption 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.
More information can be found in this answer.
In my case, the error was just fatal: refusing to merge unrelated histories on every try, especially the first pull request after remotely adding a Git repository.
Using the --allow-unrelated-histories flag worked with a pull request in this way:
git pull origin branchname --allow-unrelated-histories
As per 2.9.0 release notes - git pull has been taught to pass the --allow-unrelated-histories option to underlying git merge
github - How to fix "refusing to merge unrelated histories"? I have tried "git pull origin master --allow-unrelated-histories" - Stack Overflow
I need to understand Git pull origin master --allow-unrelated-histories better - Stack Overflow
How am I supposed to merge these two? when I try to git merge master, I get told "refusing to merge unrelated histories"
Fix fatal: refusing to merge unrelated histories on git pull
You can use --allow-unrelated-histories, as morty answered. But watch out: this may not do what you want. It depends on what you want, and what's in the two commits you'll choose this way.
git merge works by comparing the merge base commit with the two particular other commits that you select. You select one of the two commits you want to use by checking it out:
git checkout master
and the other with an argument to git merge:
git merge wip269
for instance.
The merge base, however, is determined by the history, and history, in Git, consists of the commits in the repository, as linked by those commits themselves. This is where the command is failing, because the histories are not related to each other: there is no merge base commit.
If the histories were related, this:
A--B--...--H <-- master
/
...--o--*
\
P--Q--...--W <-- wip269
might be a good way to draw the commit graph. From this drawing, you could see that the history in master starts (or ends) at commit H and works backwards to A, and then to commit * and further back; and history in wip269 starts (or end) at commit W, then works back to P and on to * and further back.
Commit * would then be the merge base. The merge base is defined as the best common commit. The best one is obviously *—commits that come before it would work too, but it seems better to stick with the one closest to the two branch-ends you started from. The way git merge works is to compare the files in the merge base to the files in your current commit—here H—to see what you changed, then compare those same files in the same merge base to the files in their commit (W) to see what they changed. Having found what you both changed, Git can combine the changes.
The problem here is that your history does not look anything like this. It probably looks something like this:
A--B--...--H <-- master (HEAD)
P--Q--...--W <-- wip269
That is, starting at H and working backwards, Git eventually arrives at commit A, which is a root commit: it has no previous history. Meanwhile, starting at W and working backwards, Git eventually arrives at commit P, which is also a root commit. There is no shared commit that is on both branches.
Using --allow-unrelated-histories tells git merge: OK, if there's no common commit, play a pretend game that there is: pretend that there is a common commit that contains no files at all, and use that as the merge base.
This means that what you changed is: you invented every file from scratch. Meanwhile, what they changed is that they, too, invented every file from scratch.
Where the invented files have different names, Git will take the new files. Where they have the same names, Git will declare an add/add conflict and leave you to figure out what should go in the file with that name.
If and when you resolve all such conflicts and commit—or if there are no conflicts—Git will make a new merge commit whose parents are both H and W:
A--B--...--H
\
X <-- master (HEAD)
/
P--Q--...--W <-- wip269
and now commits P through W are on both branches. Commits A through H, plus X, are (only) on master. If you move the name wip269 so that it, too, points to new commit X, all 17 commits will be on both branches; or you can delete the name wip269 now, if you're done with it, after which all commits are only on master.
Side note on git pull
All git pull does is run git fetch, then immediately run a second Git command. The second command is usually—and is in your case—git pull. So you can do this with git pull (which can pass --allow-unrelated-histories to its git merge), but you might as well do it with git merge at this point—you've probably already done any necessary fetch.
dev and master branches have 'unrelated histories' which means they have no common base by default.
git merge does not allow two branches with unrelated histories to be merged to prevent parallel histories.
You can use --allow-unrelated-histories to force the merge.