67
votes

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?

5
What did you do to setup your local repository? Did you run git init + git remote add ...? What is the remote and what are you trying to do?max630
A branch points to a commit. A non-root commit has one or more parents and each parent commit has its own one or more parents. When two branches don't have any common ancestor in their histories, they are unrelated.ElpieKay
Thank you for your comments! I just wanted to know what 'unrelated history' not to resolve my problem.Byeongin Yoon
There is a better and more inclusive answer here stackoverflow.com/questions/37937984/…Abednego

5 Answers

124
votes

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
11
votes

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.

4
votes

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
0
votes

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.

0
votes

git pull origin master --allow-unrelated-histories