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:
- fork a repo on github to my own github repo using the github ui.
- clone the repo locally thusly:
- git clone -b develop https://github.com/me/aproject.git
- Now someone made some changes on the original repo I forked form, so I add it as an origin:
- git remote add upstream https://github.com/original/aproject.git
- git remote -v to check I see both remotes (my fork and the forkee)
- git fetch --all
- git status (shows I am on develop)
- git checkout develop (already on develop)
- 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.