How to Fix Git Refusing to Merge Unrelated Histories Issue

A simple guide to resolving the error when merging branches with no common ancestor in Git.

When I tried to merge a git branch from a different repository, I got the following error:

% git merge development-branch
fatal: refusing to merge unrelated histories

Git is refusing to merge the branch because the histories do not share a common ancestor.

The error message fatal: refusing to merge unrelated histories means that the two branches have completely separate histories.

Luckily, this is easy to fix. You just need to add the --allow-unrelated-histories option to your merge command.

According to the official documentation:

By default, the git merge command refuses to merge histories that do not share a common ancestor. This option can be used to override this safety when merging histories of two projects that started their lives independently. As that is a very rare occasion, no configuration variable to enable this by default exists or will be added.

Here’s an example:

% git merge development-branch --allow-unrelated-histories
Merge made by the 'ort' strategy.
 infix-postfix-prefix/README.md                                 |    9 +
 infix-postfix-prefix/build.xml                                 |   74 ++
 infix-postfix-prefix/manifest.mf                               |    3 +
 infix-postfix-prefix/nbproject/build-impl.xml                  | 1411 +++++++++++++++++++++++++++
 infix-postfix-prefix/nbproject/genfiles.properties             |    8 +
 infix-postfix-prefix/nbproject/project.properties              |   73 ++
 infix-postfix-prefix/nbproject/project.xml                     |   16 +
 .../src/net/junian/mathnotationconv/JMainFrame.form            |  458 +++++++++
 .../src/net/junian/mathnotationconv/JMainFrame.java            |  513 ++++++++++
 .../src/net/junian/mathnotationconv/JNotation.java             |  218 +++++
 .../src/net/junian/mathnotationconv/JStack.java                |   52 +
 11 files changed, 2835 insertions(+)
 create mode 100644 infix-postfix-prefix/README.md
 create mode 100644 infix-postfix-prefix/build.xml
 create mode 100644 infix-postfix-prefix/manifest.mf
 create mode 100644 infix-postfix-prefix/nbproject/build-impl.xml
 create mode 100644 infix-postfix-prefix/nbproject/genfiles.properties
 create mode 100644 infix-postfix-prefix/nbproject/project.properties
 create mode 100644 infix-postfix-prefix/nbproject/project.xml
 create mode 100644 infix-postfix-prefix/src/net/junian/mathnotationconv/JMainFrame.form
 create mode 100644 infix-postfix-prefix/src/net/junian/mathnotationconv/JMainFrame.java
 create mode 100644 infix-postfix-prefix/src/net/junian/mathnotationconv/JNotation.java
 create mode 100644 infix-postfix-prefix/src/net/junian/mathnotationconv/JStack.java

That’s it! If there are any merge conflicts, resolve them and then push your changes.

Hope this helps. Thanks for reading!

References