Merge error refusing to merge unrelated histories

Understand and learn how to quickly fix the 'fatal: refusing to merge unrelated histories' Git error.

What is the ‘fatal: refusing to merge unrelated histories’ error?

The fatal: refusing to merge histories error is a fairly common Git error. It appears when a developer tries to merge two unrelated projects into a single branch.

This error appears when the branch has its commit histories and tags incompatible with the pull request or clone.

Why does ‘fatal: refusing to merge unrelated histories’ happen?

Here are some common scenarios where fatal: refusing to merge unrelated histories can occur.

  1. You have a new Git repository with some commits. You then try to pull from an existing remote repo. The merge becomes incompatible because the histories for branch and remote pull are different. Git sees the situation as you trying to merge two completely unrelated branches, and it doesn’t know what to do.
  2. There’s something wrong with the .git directory. It may have been accidentally deleted at some point or got corrupted. This can happen if you’ve cloned or cleaned a project. Here the error occurs because Git doesn’t have the necessary information about your local project’s history.
  3. The branches are at different HEAD positions when you try to push or pull data from a remote repo and cannot be matched due to a lack of commonality.

How to resolve ‘fatal: refusing to merge unrelated histories’

There are two ways of solving the fatal: refusing to merge unrelated histories error.

Option 1: Use ‘–allow-unrelated-histories’

One way to solve the issue is to use the --allow-unrelated-histories git flag.

Here the git command will look something like this: git pull origin master --allow-unrelated-histories.

You can substitute origin with the remote repository you are pulling from. You can also replace the master branch with whatever branch you want the pull request to merge into.

The idea behind --allow-unrelated-histories is that git lets you merge unrelated branches. This git flag works seamlessly when there are no file conflicts.

However, in reality, at least one thing pops up, and you will need to use the normal Git resolution flow to resolve them.

Here is an example of what a common conflict may look like when trying to merge branches:

 Auto-merging package.json
 CONFLICT (add/add): Merge conflict in package.json
 Auto-merging package-lock.json
 CONFLICT (add/add): Merge conflict in package-lock.json
 Auto-merging README.md
 CONFLICT (add/add): Merge conflict in README.md
 Auto-merging .gitignore
 CONFLICT (add/add): Merge conflict in .gitignore
 Automatic merge failed; fix conflicts and then commit the result.

Option 2: unstage, stash, clone, unstash, and then commit

The alternative (and longer) way of fixing the fatal: refusing to merge unrelated histories issues is to unstage your current commits, stash them, clone your required remote repository, and then place your stashed branch contents into the new clone.

This will ensure that any conflicts that you may encounter in the code are addressed before merging and prevent application errors from occurring.
To unstage all the files in your last commit, use the following git command: git reset HEAD~.

To stash your unsaved files, use the following git command: git stash.

This will give you a clean working tree to pull your remote repository into. Once you’ve successfully pulled into your branch, you can unstash your files, commit them as a separate commit and resolve any file conflicts that you may have.

To unstash your files, use git pop. This will move the changes stashed and reapplies them to your current working copy.

Alternatively, you can use git stash apply to add the changes to your current working copy of code.

Here is a quick summary of differences between git stash apply and <code”>git pop:

  • git pop: ‘pops’ the changes from the stash and applies them to the current code
  • git stash apply: keeps the changes in the stash and applies the changes to the current code

How to prevent ‘fatal: refusing to merge unrelated histories’

The easiest way to prevent the fatal: refusing to merge unrelated histories error is to avoid pulling remote repositories into branches that already have commits on them.
However, sometimes you just want to keep the commits. One way to prevent the error is to create a brand new branch, pull your required code in, and then manually merge your local branch into your main flow.

Here is a git example of the flow:

# branch A is where your current code is
# clone in your remote repo into a new and separate branch. 
# For our purposes, it's branch B
 ​
git clone -b [branch] [remote_repo]
 ​
# to merge A into B, you need to be on B
# merge your branches together
git merge A

The only thing about merges is that if there is a conflict in the code, there is no way around it other than resolving it as usual.
Here’s what your merge branch looks like on Git:

          C1---C2---C3 branch A
                      
  Ca---Cb---Cc---Ce---Cf branch B

Kubernetes Troubleshooting with Komodor

The above guide should help you through the troubleshooting steps you need to follow in order to resolve the fatal: refusing to merge unrelated histories Git error.

This is, of course, just one of many Git errors that you may come across in your K8s logs. Such deceptively minor and often easy-to-miss issues can cause serious issues and detecting them could be time-consuming, stressful, and often downright impossible.

This is why we created Komodor. Acting as a single source of truth (SSOT), Komodor streamlines and shortens k8s troubleshooting processes. Built by devs for devs, Komodor offers:

  • Change intelligence: Every issue is a result of a change. Within seconds we can help you understand exactly who did what and when.
  • In-depth visibility: A complete activity timeline, showing all code and config changes, deployments, alerts, code diffs, pod logs and etc. All within one pane of glass with easy drill-down options.
  • Insights into service dependencies: An easy way to understand cross-service changes and visualize their ripple effects across your entire system.
  • Seamless notifications: Direct integration with your existing communication channels (e.g., Slack) so you’ll have all the information you need, when you need it.

If you are interested in checking out Komodor, use this link to sign up for a Free Trial.

When developers start using Git as a version controlling system for their projects, they encounter many fatal errors, one of which is “fatal: git refusing to merge unrelated histories”. The solutions to these fatal errors are often complex and situation-based.

However, the error we are talking about in this article is not that difficult to resolve. There is a simple, straightforward solution to it which we will be discussing in this brief article. Also, we will investigate the reasons that cause the error occurrence.

What are the Causes of the “fatal: refusing to merge unrelated histories” Error?

This error appears when some of your ambiguous actions confuse Git on how to function further. Typically, when you try to merge two unrelated projects with different commit histories into a single branch, the error pops up. The two histories will remain incompatible since they don’t have common tags and commit message.

Following are the two main scenarios that can raise the discussed fatal error:

        • You create a remote repository that has its own history in the .git directory, and then you create a local git repository that has a different .git directory of its own with the history of your local commits.
        • Later, after making several commits, you try to pull or merge this local repository to the remote one, and boom, you are hit by the error.

          Why so? Because the repositories that you are trying to connect have different .git directories and hence different histories, and Git does not know how these two projects are related to each other.

      • You are also vulnerable to this fatal error if the .git directory inside your project has been corrupted or deleted. In that case, Git loses track of the project because of the absence of history.

How to fix “fatal: refusing to merge unrelated histories” Error?

The solution to this fatal error is to use –allow-unrelated-histories flag with the git pull command or git merge command. Your command will somehow look like the following:

Git pull origin master –allow-unrelated-histories 

Here, the origin is to be replaced with the name of your remote repository and master with the branch name that you want to work in.

Conclusion

In this brief yet convenient guide, we discussed one of the most commonly faced Git fatal errors caused by nothing hugely wrong but by little ambiguity in the workflow. The error is simple to resolve with the help of one additional flag embedded with the common Git commands.

We discussed the solution and the causes of the error, and if you are falling in one of the discussed scenarios, this guide must be a go-to way to your problem’s solution.

Read also:

  • Fix: ‘git’ is not recognized as an internal or external command, operable program, or batch file error? [Solved]
  • Git Prune: Command to Clean Up Local Branches
  • How to Uncommit Changes in Git? [Complete Guide]
  • How to Undo a Git add

After using Git for a while, you’ll notice that there’s a lot of mistakes that can be difficult to fix. This isn’t the case with the fatal: refusing to merge unrelated histories error. Unlike a number of other errors, this issue has one solution that you can reuse whenever you encounter it.

In this guide, we’re going to talk about how to solve the fatal: refusing to merge unrelated histories errors in Git. Let’s get started!

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest

First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

Why Does the Error Occur?

The error fatal: refusing to merge unrelated histories occurs when two unrelated projects are merged into a single branch. This error arises because each project has its own history of commits and tags. These two histories will be incompatible with each other.

There are two main scenarios when this error may arise.

When you create a new repository, make a few commits, and try to pull from another remote repository, this error can occur. This error will be displayed because the local repository with which you are working will have a different history to the project which you are trying to retrieve.

You may encounter this error if the .git directory inside a project that has been deleted or corrupted. In this case, the Git command line may not be able to read your local project’s history.

When you try to push data to or pull data from a remote repository, this error will occur. This is because Git doesn’t know if the remote repository is compatible with your current repository.

How to Solve ‘fatal: refusing to merge unrelated histories’

We’ve done enough talking. To solve this issue, you can use the --allow-unrelated-histories flag when pulling data from a repository:

Git pull origin master –allow-unrelated-histories

You’ll want to substitute origin with the remote repository from which you are pulling resources. You should replace master with the branch that you want to retrieve.

This command was added to handle the rare event that you are working with two projects that have their own branches. 

Alternatively, you could clone a new version of the remote repository using git clone and start over. However, this is not usually necessary.

You can read more about the –allow-unrelated-histories flag on the official Git documentation.

Conclusion

The fatal: refusing to merge unrelated histories error occurs when either a .git directory is unreadable or when you are trying to pull data from a repository with its own commit history. This error tells you that you are trying to Git merge two unrelated projects to the same work tree.

Now you’re ready to solve this error using the –allow-unrelated-histories flag like an expert!

  1. Common Scenarios of the fatal: refusing to merge unrelated histories Error in Git
  2. Resolve the fatal: refusing to merge unrelated histories Error in Git

Fatal: Refusing to Merge Unrelated Histories Error in Git

This article outlines the steps needed to solve the fatal: refusing to merge unrelated histories error in Git. We usually encounter such an error when attempting to merge two unrelated Git projects into one branch.

It will pop up when the recipient branch has commits, and the clone has incompatible tags.

Here are some cases where you will likely encounter the fatal: refusing to merge unrelated histories error in Git.

  1. You have a local repository containing commits and attempt to pull from an existing remote repository. Since the histories are different, Git does not know how to proceed.

    Hence the error message.

  2. Your .git directory is corrupted.

  3. When your branches are at different HEAD positions.

We can resolve the error by allowing unrelated merging. We can do this by adding the --allow-unrelated-histories flag to the pull request, as shown below.

$ git pull origin master --allow-unrelated-histories

Git will allow you to merge branches with unrelated histories. It is pretty easy if your files do not conflict.

The above method is the easiest; however, there is a longer route to the same destination.

First, you will have to unstage all your current commits and stash them. You can then pull from the remote repo and apply the stash to the new clone.

This makes it easier to deal with merge conflicts in case they occur.

To unstage, all the files in the last commit, use the command below.

Run the command below to stash the files.

Now you have a clean working tree, and you can run a pull request, after which you will unstash and apply the stashed changes to the working tree. You can run:

The command above will apply the stash and discard it. If you want to apply and keep the stash, you can run:

With that being said, what is the easiest way to avoid this error message?

Avoid pulling remote repositories into local branches containing commits. If you have to, create a new branch and manually pull and merge the changes.

In conclusion, the fatal: refusing to merge unrelated histories error occurs when we are attempting to merge branches with different commit histories. We have gone through the two ways to resolve the error and how to best avoid it.

The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged, and two projects are unaware of each other existence and having mismatch commit histories.

Git Fatal Refusing To Merge Unrelated Histories
Scenario Git fatal: refusing to merge unrelated histories

To provide a little background .git directory, which is usually hidden, contains all the changes or “commits” of the repo that gets tracked. Rewriting the repository history is possible, but it’s generally not the typical use case. Git is used for version control, which means to track the history of the file.

Use Cases that lead to git fatal: refusing to merge unrelated histories

  1. If you have cloned a repository and, for some reason, the .git folder is corrupted or deleted. Since git will be unaware of your local history, any action you perform like git pull or git push to remote repository will throw this error as there is no tracking information for the current branch.
  2. You have created a new repository, made few commits to it, and now try to pull a remote repository that already has its own commits. Git will throw an error here since it is unaware of how these two projects and commits are related.

The solution to Refusing to merge unrelated histories 

The error started occurring from git version 2.9.0 release notes and above. To solve this issue --allow-unrelated-histories flag when pulling the data from remote repository.

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

Avatar Of Srinivas Ramakrishna

Srinivas Ramakrishna is a Solution Architect and has 14+ Years of Experience in the Software Industry. He has published many articles on Medium, Hackernoon, dev.to and solved many problems in StackOverflow. He has core expertise in various technologies such as Microsoft .NET Core, Python, Node.JS, JavaScript, Cloud (Azure), RDBMS (MSSQL), React, Powershell, etc.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.

Понравилась статья? Поделить с друзьями:
  • Mercury 231 ошибка e 01
  • Mercury 130f ошибка 478
  • Mercury 130f ошибка 025
  • Mercurial error 255
  • Mercenaries 2 world in flames не сохраняется как исправить