How to Solve Error Message fatal: refusing to merge unrelated histories upon executing ‘git pull’

Posted on

Introduction

This is an article for solving the error message as it exist in the title of this article. The error message is ‘fatal: refusing to merge unrelated histories’. The error message appear upon executing the command of ‘git pull’. Actually, the complete command is ‘git pull origin master’. The background of the command execution is because the default branch is set into another branch. That branch is not the ‘master’ branch but it is actually another branch. So, executing the first command for cloning the git-based repository will actually clone that default branch. Furthermore, not all of the source files exist after the cloning process. Since it is not exist in the first clone process, the further attempt will continue on to pull the source code available in the ‘master’ branch. So, the next command execution is in the following command :

git pull origin master

The following is the actual output of the above command execution :

user@hostname:~$ git pull origin master
Username for 'http://www.gitlab.com': myuser
Password for 'http://myuser@www.gitlab.com': 
From http://www.gitlab.com/myuser/myapp/mobileapp
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories
user@hostname:~$ 

Apparently, it is not working and it is triggering the following error message as in the above output command execution :

fatal: refusing to merge unrelated histories

Solution

So, continue on the previous section, in order to solve the problem by pulling all of the available source codes in the ‘master’ branch, the following is the solution to handle it :

git pull --allow-unrelated-histories

The following output is the execution of the above command :

user@hostname:~/mobileapp$ git pull origin master --allow-unrelated-histories
Username for 'http://www.gitlab.com': myuser
Password for 'http://myuser@www.gitlab.com': 

Apparently, after successfully authenticate the above process, the following output will appear :

How to Solve Error Message fatal: refusing to merge unrelated histories upon executing 'git pull'
How to Solve Error Message fatal: refusing to merge unrelated histories upon executing ‘git pull’

Actually, executing the above command will merge two different branch. The first branch is the default branch which is not the ‘master’ branch. The other one is the ‘master’ branch. As in the above image, there is an information of ‘Merge branch ‘master’ of the ‘http://www.gitlab.com/gitlab/myapp/mobileapp’ into the other branch. Since it is not in the same branch, and in this context it doesn’t have any relation, once again, ‘MAKE SURE THAT EACH BRANCH DOES NOT RELATED’. Because this process will merge the source codes from each branch. In detail, since the pulling process is for the ‘master’ branch, the complete command will be as follows :

git pull origin master --allow-unrelated-histories

Fortunately, it works. The following is the actual output of the command execution of the above command pattern :

 

user@hostname:~/mobileapp$ git pull origin master --allow-unrelated-histories
Username for 'http://www.gitlab.com': myuser
Password for 'http://myuser@www.gitlab.com': 
From http://www.gitlab.com/myuser/myapp/mobileapp 
 * branch              master          -> FETCH_HEAD
Merge made by 'recursive' strategy.
...
...
...

If there is no other error appear, the above command execution will merge all the branches.

Leave a Reply