How to deal with "refusing to merge unrelated histories" error

Short version of my question : For years, I have been using a simple, single one-branch, one-contributor public online Github repo. A few days ago my computer died suddenly and I bought a new one. Now Github refuses to connect the local repo
from my new computer to the online repo saying “refusing to merge unrelated histories”. What do I do ?

Long version of my question : here is the exact sequence of commands that I did in my new computer :

Step 1 : Download git, use git config to tell git about my username & email. Worked fine.

All the following command were executed in my new local repo’s main directory

Step 2 : git init. Worked fine.

Step 3 : Do a git add on all my files. Worked fine.

Step 4 : Do the first commit : git commit -m "First commit from new computer". Worked fine.

Step 5 : Do git remote add origin [https://github.com/roparzhhemon/myremoterepo.git](https://github.com/roparzhhemon/myremoterepo.git). Worked fine, according to
git remote -v.

Step 6 : git push. Got the following error message :

fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master

Step 7 : Do as I’m told, and type : git push --set-upstream origin master. Got the following error message :

error: failed to push some refs to [remote repo]
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Step 8 : Do as I’m told, and type : git pull. Got the following message :

warning: no common commits
remote: Counting objects: 11450, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 11450 (delta 13), reused 17 (delta 8), pack-reused 11425
Receiving objects: 100% (11450/11450), 1.96 MiB | 2.65 MiB/s, done.
Resolving deltas: 100% (7710/7710), done.
From [remote repo]
* [new branch] master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master

Step 9 : Do as I’m told, and type : git branch --set-upstream-to=origin/master master. Seemed to work, output the following : 

Branch 'master' set up to track remote branch 'master' from 'origin'.

Step 10 : Try git pull again. Got the error message :

fatal: refusing to merge unrelated histories

Step 11 : Try git push again. Got the error message :

To [remote repo]
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to [remote repo]
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
37 Likes

If you just start a new repository, the history will be completely different than GitHub knows it.

Use

git clone <repo>

and just make commits on top of that. Then push.

38 Likes

I ended in the same situation after a series of mishapps. Have a look at this thread : I just did a 

git pull --allow-unrelated-histories

and it was fixed!

1257 Likes

 I was also facing the same problem. But I realised I was trying to push my one git project material on other remote git project. Solution:

Clone the fresh project again and this time make sure cloning link is starting from ssh if you have added the SSH key and https:// if you have added the puttyKey.

2 Likes

Thanks, worked fine for me.

20 Likes

I am in a similar situation where I am getting the same messages. Probably I just need to do the

--allow-unrelated-histories

But I wish I knew what situations where I should not. Obviously we get the message because there might sometimes be a good reason to not do the allow. I got into my situation because I did not know what I was doing and did something I should not have.

31 Likes

I am receiving the message saying “there is no tracking information for the current branch”

5 Likes

Help for me. thanks!

1 Like

Thank, you for you help.

git pull --allow-unrelated-histories

worked

3 Likes

This is great, thank you very much!

http://gph.is/1U7OwpV

1 Like

This command is a dangerous one. “git pull --allow-unrelated-histories” it has messed up my whole codebase i dont know what happened and am not in a position to recover from it

1 Like

It’s worked for me.
Very thanks! i have solved my problem

4 Likes

Thanks, It works for me.

Works for me too ! thank you :slight_smile:

Thanks, Woked superbly for me as well.
but little more specific:

git pull <remote> <branch> --allow-unrelated-histories

4 Likes

Worked like a charm

I got this error, but found another cause:

somehow, the ssh url of another project got into this one’s .git/config file.

So before running this to accept ‘unrelated histories’, you may want to confirm you have the right repo’s url.

I fixed mine and it started working again.  :slight_smile:

1 Like

Probably because you did a git init on something like “the same” set of files locally, then tried to merge with the files on the repository. The better way is to clone from the repository, then work from that.

3 Likes

I tried the --allow-unrelated-histories and got this:

warning: Cannot merge binary files: biostats.final.3.2.pdf (HEAD vs. 93379526ed908abd4f58b2010eb055aace5df522)

warning: Cannot merge binary files: .DS_Store (HEAD vs. 93379526ed908abd4f58b2010eb055aace5df522)

Auto-merging <file name here>

CONFLICT (add/add): Merge conflict in <file name here>

Auto-merging <file name here>

CONFLICT (add/add): Merge conflict in  <file name here>

Auto-merging  <file name here>

CONFLICT (add/add): Merge conflict in  <file name here>

Auto-merging  <file name here>

CONFLICT (add/add): Merge conflict in  <file name here>

Auto-merging  <file name here>

Automatic merge failed; fix conflicts and then commit the result.