Click here to Skip to main content
15,903,388 members
Articles / DevOps / Git

Git – Error: Refusing to Merge Unrelated Histories

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
14 Aug 2022CPOL2 min read 7.2K   2   7
We are showing how to overcome Git errors and merge unrelated histories
Your Git tool might sometimes refuse to merge branches, with the error: “fatal: refusing to merge unrelated histories”. We are showing how to overcome the problem.

1 Problem

Sometimes during work with GitHub, you will get into a situation where you created initial branches on both sides, local and remote. Naturally, you would like to merge the branches, but your Git tool might refuse to merge them because branches have “unrelated histories.” I will show the problem that appeared when I was using GitHub and SourceTree but can happen with other tools and remote repositories.

The problem that I had, that just happened, but I am sure will happen again to me and to other people, was that I during work finished with two initial branches created: main and master. The “main” branch is by default created by GitHub and there is a readme file there. The branch “master” is created by default by SourceTree and my code is in it.

Image 1

Ok, there is no need to have two branches here, let’s merge them and get rid of one of them. Let’s keep GitHub default branch “main” and delete “master.”

But there is a problem when you try to merge those two branches:

Image 2

It gives an error: “fatal: refusing to merge unrelated histories

2 Solution

Since our two branches do not have a common ancestor commit, SurceTree thinks we are doing something wrong. But we know we are right and want to force it.

2.1. Forcing merge

I looked and it says in [1] that you cannot resolve that from SourceTree GUI. So, we need GitBash to run the command line Git commands. Explanation of problem and instructions are at [2].
We need to run “git merge <branch-name>  --allow-unrelated-histories

So, you can open GitBash from SourceTree (Terminal button), and execute the command. You will be prompted to enter a commit comment in the separate text editor. Then merge will continue.

Image 3

And here is what merged branches now look like:

Image 4



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Serbia Serbia
Mark Pelf is the pen name of just another Software Engineer from Belgrade, Serbia.
My Blog

Comments and Discussions

QuestionBroken Images Pin
Graeme_Grant12-Aug-22 10:15
mvaGraeme_Grant12-Aug-22 10:15 
AnswerRe: Broken Images Pin
Mark Pelf 12-Aug-22 10:24
mvaMark Pelf 12-Aug-22 10:24 
GeneralRe: Broken Images Pin
Graeme_Grant12-Aug-22 11:37
mvaGraeme_Grant12-Aug-22 11:37 
GeneralRe: Broken Images Pin
Mark Pelf 12-Aug-22 19:09
mvaMark Pelf 12-Aug-22 19:09 
GeneralRe: Broken Images Pin
Graeme_Grant12-Aug-22 19:28
mvaGraeme_Grant12-Aug-22 19:28 
GeneralRe: Broken Images Pin
Graeme_Grant12-Aug-22 19:34
mvaGraeme_Grant12-Aug-22 19:34 
GeneralRe: Broken Images Pin
Mark Pelf 12-Aug-22 20:07
mvaMark Pelf 12-Aug-22 20:07 
OK, I can do that in the future, but I was doing that in other articles, and it didn't make problems. Look at Git – Push from GUI Client Returns "Authentication Failed"[^] , there are images named in the same way.
Their WebAdmin needs to look into it, I see some image CSS classes in HTML that would suggest caching enabled...lazy load... but they know for sure what they are doing..

modified 13-Aug-22 2:55am.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.