Book Your Free Call

How To Fix "fatal: refusing to merge unrelated histories" in Git?

Tim Mouskhelichvili
Tim Mouskhelichvili
2 minutes to read

Sometimes, when a developer merges two projects, Git outputs the "fatal: refusing to merge unrelated histories" error. This error happens when Git tries to merge two project branches without a common base. Luckily, Git offers an easy command to fix this error.

To fix the "fatal: refusing to merge unrelated histories" error in Git, use the git pull command like so:

bashgit pull origin main --allow-unrelated-histories

This article will explain why this error happens and give a method to fix it.

Let's get to it 😎.

git refusing to merge unrelated histories

Why does this error happen?

The "fatal: refusing to merge unrelated histories" error happens when two projects with mismatching commit histories or not aware of each other's existence are merged.

This error can happen for many reasons.

Here are a few of them:

  • The .git directory is corrupted or deleted.
  • You have a new repository with a few commits.
  • The branches have different HEAD positions.
  • When setting up a different remote for a local repository.

How to fix this error?

By default, Git doesn't allow merging two branches with no common base (since version 2.9).

That's why Git introduced the --allow-unrelated-histories option.

The --allow-unrelated-histories option overwrites the default behavior and forces the merge to happen.

To fix the "fatal: refusing to merge unrelated histories" error, toggle the allow unrelated histories option on the git pull command, like so:

bashgit pull origin main --allow-unrelated-histories

Note: The git pull command is a shorthand for git fetch and git merge. That's why when you pull, Git also merges. I've written an extensive guide on git fetch vs git pull (if you are interested).

How to prevent this error?

This error is hard to prevent because the merge happens out of necessity most of the time.

The best a developer can do is know why it happens and how to fix it.

Final thoughts

In conclusion, understanding how to fix this error is very important for developers because this error happens often.

Next time you see this error, you'll know what to do.

git refusing to merge unrelated histories

Here are some other Git tutorials for your enjoyment:

Comments (0)