fatal: refusing to merge unrelated histories
I get this error every now and then. This happens when I created a new branch first right after a new Git project was started (by me), without bothering to base it from the Master branch. The reason is the lack of permission/access to being able to write or put anything on the latter branch.
Rather than waiting for someone to kick off the Master branch, I just go ahead and start my own branch as I’ve mentioned earlier. Later on, somebody from the team who has write permissions to Master, starts a README file just to initiate it. Thus the “unrelated histories” issue happens from then on. Two branches that started off independently from each other and with no common base.
The issue appears when I want to merge my branch to Master. Git will say my branch is behind on commit on the branch it will be merged into. If I try to merge it, it gets rejected. The pipeline also won’t allow for merge conflicts like this, and advises me to fix it first.
As I recall, this behavior started after a certain Git version only. This to avoid to unnecessarily create a parallel history being merged into the project, because a previous merge happened between to branches that didn’t have a common base.
I’ve used Git’s “–allow-unrelated-histories” option to fix this problem. I’ll do this via command line.
Pull the Master branch into mine with the option above included.
Fix the merging conflict.
Commit the changes.
Push to remote repository.
Then this time the merge request to Master will not warn that my branch is behind a commit.
Others have commented saying that rebasing also does the trick for them. I do not recall having gone that route though.