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

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.

Worked for me. Thank you and God Bless! :slight_smile:

This comment should be deleted or at least not appear as first, See the accepted answer.

75 Likes

I just found that this git push problem really ruined a lot of my code. It is very very sad and frustrating. This is a huge problem for me and I wish I knew what to do to prevent it.

Its work for me. Thanks 

Managed by avoiding creating the repository with a Readme.

  1. crete without readme.md

  2. the ‘create-react-app’ command already have a ‘Readme.md’ file.

  3. Modify teh readme.md later on.

Thanks,

Arun

thanks alot… ^^

Undebatably the most useful solution ever to the most non-sensical error message ever.

thank u! work perfect

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.

46 Likes