Git refusing to merge unrelated histories. What is 'unrelated histories'?

Git

Git Problem Overview


In my local, I made new text file -> git add newfile.txt -> commit -> pull origin master -> ERROR!

"refusing to merge unrelated histories".

What is unrelated histories? , what is related histories?

Git Solutions


Solution 1 - Git

I think you have commit in remote repository and when you pull this error happen.

use this command

git pull origin master --allow-unrelated-histories
git merge origin origin/master

Solution 2 - Git

When somehow the local .git subdirectory is lost, the whole project seems to be appeared from nowhere, as all the local changes histories were contained by .git. Thus, your local changes become unrelated. That is why all the changes are called unrelated histories then.

In this situation, git merge or pull request will unable to track where you made changes to add with the remote project. Hence, " refusing to merge unrelated histories"- error occurs.

In this situation, if you try to force merge by following commands,

git pull origin master --allow-unrelated-histories

git merge origin origin/master

it will create a lot of conflicts, as it is not able to find the history of your local changes.

Solution 3 - Git

I ran into a similar problem where I brought in a branch from a second remote and wanted to merge with a branch from the first remote. This is different from the existing answers as I'm not using --allow-unrelated-histories on the pull, but on the merge.

git checkout master
git merge --allow-unrelated-histories myfunnybrancy

Solution 4 - Git

git pull origin master --allow-unrelated-histories

Solution 5 - Git

I got this issue when renaming one of repository in GitHub Enterprise, and then making same former named repo as below steps.

  1. create repo sample in remote
  2. clone it to local
  3. rename remote to old-sample
  4. create a new repo named sample in remote
  5. try to push it from local
  6. error occurred.

Weird thing is that I removed local repo but git tries to clone old one even if I try to clone by newly created repo url. The clone url is just automatically redirected to old one even if the new one is existed in remote. I don't know this cause is from server or local git process.

For this reason, the error occurs because the git process fails to compare its local commit history to remote history.

The unrelated histories could tell in above situation.

I ended up to remove renamed old repo in remote and it solved.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionByeongin YoonView Question on Stackoverflow
Solution 1 - GitYashar PanahiView Answer on Stackoverflow
Solution 2 - GitTasnim FabihaView Answer on Stackoverflow
Solution 3 - GitBrian C.View Answer on Stackoverflow
Solution 4 - GitSanjay AdhikariView Answer on Stackoverflow
Solution 5 - GitYoungjaeView Answer on Stackoverflow