Refusing to merge unrelated histories

03 Mar 2017

last modified: 03-mar-2017 (22:33)


A long time ago, my senior came to me and said I have this strange error while pulling my code from server

fatal: refusing to merge unrelated histories

I was not sure at that time what happened to his repository. We have searched a bit and solved it using --allow-unrelated-histories switch with pull command. I was unaware of the consequences and origin of this error. I added this error to my to do list for searching it later. From now onwards you will read what I have learnt.

Why unrelated histories?

As clear from the error message, it happens when some one try to merge two unrelated projects (projects which do not know about each other and commit history in one project does not match with other project.). Until now I am aware of two scenarios when this error can occur.


Unrelated histories can be fixed in two ways.

allow-unrelated-histories switch

You can simple use allow-unrelated-histories with pull. This will merge remote history first followed by your commit.

git pull origin master --allow-unrelated-histories

Clone and reset to commit

This is a little tricky, you have to recall the last commit you have pulled from before you have started updating local repository. I have sketched it on white board ( apologies for bad image quality ).

Here are the steps you need to follow.

git reset --hard #commit-hash

With this, you lose all the commits you have been making while updating local repository before the .git corrupted or destroyed and all your updates are committed in a single commit in newly cloned repo.