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`. 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.


Now try git pull --allow-unrelated-histories
then git push


Post a Comment

0 Comments