Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Ground Controller Lvl 1
Message 1 of 21

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

Solved! Go to Solution.

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.
20 Replies
Commander Lvl 3
Message 2 of 21

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

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.


- Mark
Solution
Ground Controller Lvl 1
Message 3 of 21

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

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!

Ground Controller Lvl 1
Message 4 of 21

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

Thanks, worked fine for me.

Copilot Lvl 3
Message 5 of 21

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

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.

Ground Controller Lvl 1
Message 6 of 21

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

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

Ground Controller Lvl 1
Message 7 of 21

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

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.

Highlighted
Ground Controller Lvl 1
Message 8 of 21

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

I am receiving the message saying "there is no tracking information for the current branch"
Ground Controller Lvl 1
Message 9 of 21

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

Help for me. thanks!

Ground Controller Lvl 1
Message 10 of 21

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

Thank, you for you help.