Have you ever came across this git error “fatal: refusing to merge unrelated histories”. If yes then this article is especially for you.
I got this error by doing following
- I created a Laravel project in my local.
- Also, at the same time, I created a new repo on my GitHub account.
- Then to track the project in GitHub I simply did the following :
4. and committed the changes on my local.
Now, it was time to push the changes back to my remote repo. So, I simply tried to pull the changes from my remote repo, and this error occurred.
You would have understood the cause.
Yes, it was because even though I had added the remote origin on my working folder locally, my local and remote project is not related as they both were created independently and they both have no common base, which is why they both don’t know each other’s state. And therefore the error “fatal: refusing to merge unrelated histories”.
So, whenever you are in a situation like this where you local git repo is not related to your remote git repo and still you want to sync or push your changes across remote repo, you can do this
git pull origin master --allow-unrelated-histories
More on this at Git official documentation