Refusing to merge unrelated histories03 Mar 2017 Git #git
A long time ago, my teammate came to me and said I have this strange error while pulling my code from server
I was not sure at that time what happened to his repository. We have searched a bit and solved it using
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.
- You have cloned a project and some how you have lost your
.gitdirectory or somehow
.gitgot corrupted. You local history is gone and
gitis not happy with it.
- You have create a brand new repository, added few commits and now you are trying to
pullfrom some remote repository which already has some commits.
Unrelated histories can be fixed in two ways.
You can simple use
allow-unrelated-histories with pull. This will merge remote history first followed by your commit.
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.
Copy the changes you have made to some other directory.
Clone the repository again.
Reset to the commit from where you have started changes.
Paste you changes to this newly clones repository.
Commit new changes.
Sync with remote.
With this, you lose all the commits you have been making while updating local repository before the
.gitcorrupted or destroyed and all your updates are committed in a single commit in newly cloned repo.