DEV Community

Cover image for Git error “fatal: refusing to merge unrelated histories” explanation
William L'Archeveque
William L'Archeveque

Posted on

Git error “fatal: refusing to merge unrelated histories” explanation

When two unrelated projects are merged, you may encounter the fatal: refusing to merge unrelated histories Git error. This happens when projects are not aware of each other’s existence and have mismatching commit histories.


We may consider two cases that throw this :

  1. You may have cloned a project for which the .git directory is deleted or corrupted. In this situation, Git is unaware of your local history and will throw this error when trying to push to or pull from the remote repository.

  2. You may have created a new repository and added commits to it. Now you are trying to pull from a remote repository that already exists and already has commits. Git will throw the error in this case, because it does not know how the two projects are related.


"Refusing to merge unrelated histories" solution

You can solve this issue by adding the allow-unrelated-histories flag. After executing git pull or git merge, add the following flag:

git pull origin master --allow-unrelated-histories

Git provides more information regarding this :

https://github.com/git/git/blob/master/Documentation/RelNotes/2.9.0.txt#L58-L68

Cover Image : Markus Spiske (@markusspiske) from Unsplash

Top comments (1)

Collapse
 
isaachatilima profile image
Isaac Hatilima

I am facing this same issue. I have 3 branches, dev, release and master. I have a git action the merges release to master after I merge dev to release and I keep getting this error. Are there any future consequences to using this?