1
votes

NOTE: this is not related to other similar questions for which the answer was "git merge origin/master --allow-unrelated-histories". Please don't mark it as a duplicate of those. It's also not a clone of the same error message due to a bug in subtrees which was fixed a long time ago.

The history is related, it was the fork!

So here are the steps I did:

  1. fork a repo on github to my own github repo using the github ui.
  2. clone the repo locally thusly:
    1. git clone -b develop https://github.com/me/aproject.git
  3. Now someone made some changes on the original repo I forked form, so I add it as an origin:
    1. git remote add upstream https://github.com/original/aproject.git
  4. git remote -v to check I see both remotes (my fork and the forkee)
  5. git fetch --all
  6. git status (shows I am on develop)
  7. git checkout develop (already on develop)
  8. git merge upstream/develop fatal: refusing to merge unrelated histories

This used to work fine. It is so bazar, I deleted the entire repo locally, deleted my fork from github, created a new fork and did all the steps again, and got the same result.

When I look at my fork's repo in github UI, it says "forked from original/aproject. So git/github is aware of the related history!

Any suggestions?

git version 2.16.3

Update 1

torek recommended checking the commit hashes:

git ls-remote gives:

 1ee37f195c25d51a1159428a155062ca3acd48d7   HEAD
 1ee37f195c25d51a1159428a155062ca3acd48d7   refs/heads/develop
 5dcee17dd30b6f57d4711495934fa722a32e60a5   refs/heads/master

If I now go to the original repo I forked from, and look down the commits, I see:

1ee37f195c25d51a1159428a155062ca3acd48d7

This was committed just before I forked. Since I forked there have been further commits to develop on the repo I forked from, but I cant merge them because it says "unrelated histories" which is, I believe, wrong. Could this be a bug in git?

NOTE 2: I have done exactly these steps in the past and it has worked, but that was on a PC using git bash/tortoise. Now I am on a Mac. I dont know if this makes any difference.

I have made no changes to my local forked repo. No commits, no edits or new files (until I can resolve this issue)

UPDATE 2

I found a workaround. It seems that if I do:

git clone https://github.com/me/aproject.git
git checkout develop

Instead of

git clone -b develop https://github.com/me/aproject.git

I can then later fetch -all from the upstream remote (and origin remote) and merge into local and origin.

1

1 Answers

1
votes

Given the sequence of operations you outlined, the only way to see this effect is if the upstream rewrote all of their commits (e.g., using git filter-branch filters -- --all.

Doing this kind of thing effectively replaces the entire repository (well, can replace, depending on the precise filtering). The new repository is no longer compatible with any clone of the original repository and it's generally unwise to try to mix them. It's as if they, whoever they are, deleted their repository, and someone else came in with a different repository (that you did not fork) and put that in the same place.

You can tell whether this has happened by comparing the actual Git commit hash IDs in your fork with those in the repository you forked. Either they match, in which case the histories are related and the merge should work; or they do not match, in which case the histories are not related and they must have rewritten every commit. It is the hash IDs that matter here.