The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged, and two projects are unaware of each other existence and having mismatch commit histories.
To provide a little background .git directory, which is usually hidden, contains all the changes or “commits” of the repo that gets tracked. Rewriting the repository history is possible, but it’s generally not the typical use case. Git is used for version control, which means to track the history of the file.
Use Cases that lead to git fatal: refusing to merge unrelated histories
- If you have cloned a repository and, for some reason, the .git folder is corrupted or deleted. Since git will be unaware of your local history, any action you perform like git pull or git push to remote repository will throw this error as there is no tracking information for the current branch.
- You have created a new repository, made few commits to it, and now try to pull a remote repository that already has its own commits. Git will throw an error here since it is unaware of how these two projects and commits are related.
The solution to Refusing to merge unrelated histories
The error started occurring from git version 2.9.0 release notes and above. To solve this issue
--allow-unrelated-histories flag when pulling the data from remote repository.
git pull origin master --allow-unrelated-histories git merge origin origin/master ... add and commit here... git push origin master