How to Solve Error Message “refusing to merge unrelated histories”

Posted on

Introduction

In a certain point, there is an error upon retrieving a command. The error is in the title of this article, it is “refusing to merge unrelated histories”. Solving that error message is going to be the main focus of this article. It happens when performing the command ‘git push -u origin master’ or ‘git push origin master’ . But pushing a new added source is not working and it ends in failure. The reason is because there is already a file or a script in the repository. So, it will alert an error message saying that the file or t. he script available in the repositoy is not related with the one available in local. Since it is also the first time for pushing a new file sources, it seems there is no history of the git process before except for the first push to the repository.

After creating a new repository which in this context it is in the Gitlab repository, the following is the additional process to prepare the Gitlab repository :

1. Execute the ‘git init’ command to initialize a folder in the local machine as a local git repository :

user@hostname:~$ git init
Initialized empty Git repository in /home/user/.git/
user@hostname:~$

2. Create a new file with the name of ‘README.md’ in the local git repository as follows :

user@hostname:~$ touch README.md

3. Add that new created file by executing the following command :

user@hostname:~$ git add . 

4. Commit the newly added file by performing the command below :

user@hostname:~$ git commit -m "Initial commit"
[master (root-commit) 60a0404] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
user@hostname:~$
user@hostname:~$ git push -u origin master
user@hostname:~$ git remote --v
origin  http://my.repository.app.com/gitlab/app/myapp.git (fetch)
origin  http://my.repository.app.com/gitlab/app/myapp.git (push)
user@hostname:~$ git push -u origin master
Username for 'http://my.repository.app.com': user
Password for 'http://my.repository.app.com': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 214 bytes | 214.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://my.repository.app.com/gitlab/app/myapp.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
user@hostname:~$ 

The above step is actually the initialization of the repository. But unfortunately in other parties, cloning the repository and also pushing a new added one generate an error. That error is the error message of ‘refusing to merge unrelated histories’.

 

Solution

There are a few attempt for resolving the problem so the error message will disappear. The following are those solutions :

  1. Create a new folder. Try to initialize that folder as a git local repository. Copy the source code or files which is going to be pushed. After that, follow the same routine by adding, committing and pushing the source code or files to the remote git repository. Unfortunately, the attempt ends in failure.

  2. Using the following command ‘git checkout master’ and followed by ‘git merge origin/master –‘allow-unrelated-histories’. That two command is actually solving the problem.

Leave a Reply