Git FAQ
Frequently asked questions around Git and Version Control.
Git FAQ featured image

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

If you attempt to merge two Git repositories with no common history, you may encounter this error message:

fatal: refusing to merge unrelated histories
Error redoing merge 292179e6bda27efb8ba9bc4d

This error occurs when Git is unable to establish a clear relationship between the branches or commits in the two repositories, thereby refusing to merge them. This change was introduced in Git 2.9 to prevent merging parallel histories into existing projects unnecessarily.

This error may arise in situations where Git lacks information on how two projects are linked, such as in the following scenarios:

  1. When you've cloned a project, but the .git directory has been deleted or corrupted. The .git directory is crucial for Git to track the complete history of the project, including commits, branches, tags, and remote repositories. Without this directory, Git cannot perform essential operations, like recognizing your local history. Consequently, it will display this error when you try to push or pull from the remote repository.

  2. When you create a new repository, add commits to it, and then attempt to pull data from a remote repository that also has its own set of commits. Git isn't aware of the connection between these two projects, so it will refuse to merge them and throw the dreaded error message.

Now that we understand the cause, let's explore the solution.

The Git Cheat Sheet

No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!

The Solution: the --allow-unrelated-histories Option

First, a word of caution: please keep in mind that merging two independent projects will result in a cluttered and disorganized Git history. It is good practice to first investigate why the two repositories have no common history and consider whether merging them is really necessary.

Would you still like to proceed? If so, the solution is simple.

To fix the error, you can use the --allow-unrelated-histories option when running the git merge command.

git merge --allow-unrelated-histories <branch-name>

This option forces Git to proceed with the merge even if it cannot find a common ancestor between the branches.

Using the --allow-unrelated-histories option will also work if you enter the git pull command, like in the following example:

git pull origin main --allow-unrelated-histories

This is possible because git pull combines git fetch and git merge. However, please note that the --allow-unrelated-histories option is only applicable to the git merge command.

Learn More

About Us

As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.

Just like with Tower, our mission with this platform is to help people become better professionals.

That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.