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

ERRRR… WRONG see accepted answer

7 Likes

THANK YOU!!

This worked for me

Thanks. You just save me.

yes,I solve mine  with this,thanks

2 Likes

To those saying this answer is wrong: It isn’t. The poster absolutely got themselves into trouble in the first place with:

$ git init
$ git add <files>
$ git commit -m "First commit from new computer"
$ git remote add origin https://github.com/roparzhhemon/myremoterepo.git

All of that should have been:

$ git clone https://github.com/roparzhhemon/myremoterepo.git

which would have automatically copied the contents of the remote repo and added it as the remote origin. You’re not meant to be doing that by hand.

If there are local files to add that aren’t in the remote repo, those can be added after the clone operation. Then the history won’t be divergent, the new commit will have the HEAD of the remote repo as its parent, and git push will go right through.

The key is right in the name of the remote: “origin”. It’s meant to be the source of your local repo’s copy of the history, not the destination for it.

The accepted answer may be the solution to this problem, when it’s already happened, but it’s much better to never get into that situation in the first place.

45 Likes

I had just created a README.md while initiating a new Repositoy. after adding remote origin the issue rised on git pull. but this is really helful to solve my problem.

This solution also works for me  :slight_smile: thanks!

1 Like

God bless you man. :slight_smile:

This worked for me too!!!

Thanks!!!

1 Like

Thank you! It works in my case.

Thanks it worked for me

Yes it worked…!! Thanks a ton :slight_smile:

Well you can try and do 

make the local repository recognize the remote

git remote add orign <giturl>

git branch --set-upstream-to=origin/master 

#sync the branches

git rebase

if any conflict resolve and continue

push to remote

git push

Thanks…

this work for me…

git clone <Repo>

git pull --allow-unrelated-histories origin master

git push -u origin master

and it works

2 Likes

Worked perfectly in VS Code

I got into this problem following a guide book that started with a local git repo and then tried to show how to go remote with a github account.  Left out was all the .ssh key stuff and this.  Thanks.  This answer worked for me, is much cleaner and doesn’t seem to be leaving me open to the problem versioning is supposed to prevent against.

if you already did a pull and you came up with master and origin/master but can’t merge them, the following command can help you:

git merge origin/master --allow-unrelated-histories 
2 Likes

its working thanks man 

Yeah, it worked for me.

Its not working when I run the command on git bash.

$ git pull --allow-unrelated-histories
Result:  fatal: not a git repository (or any of the parent directories): .git