You have received the dreaded fatal: refusing to merge unrelated histories error!

You might find yourself asking one of the following questions:

  1. What is refusing to merge unrelated histories?
  2. How do you combine two unrelated histories?
  3. What is unrelated history in git?
  4. How do you fix unrelated branches?

Well look no further, below you will learn how to solve this problem quickly and also learn about why it happens in the first place.

Tldr; Quick Solution!

git pull origin branchname --allow-unrelated-histories

This will solve the “fatal: refusing to merge unrelated histories” error; but for a deeper understanding, continue reading below!

Why does this happen?

Git is all about Deltas (the difference between 2 or more states of something), and if you have 2 histories that don’t appear to have the same bunch of things, then it won’t know what to change, or in what order.

At this point, a fatal error will be thrown stating refusing to merge unrelated histories.

It actually makes perfect sense. But how do we fix it?

How to fix merging unrelated histories

Option 1 (easy)

The easiest way is to git clone the remote repo into a new directory (~/tmp) or somewhere like that (a throwaway) and then simply copy/paste all your files that you want in there and then git push them.

Option 2 (better)

Use the --allow-unrelated-histories flag to just force it through!

Note that this could work if you know what you’re doing, but could well break everything, so use it at your own risk!

git pull origin branchname --allow-unrelated-histories

The commands to use

git pull origin master --allow-unrelated-histories
git merge origin origin/master
... add and commit here...
git push origin master