Fix "fatal: refusing to merge unrelated histories" in Git
fatal: refusing to merge unrelated histories
- git pull origin main -> fatal: refusing to merge unrelated histories
- There isn't anything to compare. main and feature are entirely different commit histories.
- fatal: refusing to merge unrelated histories (git merge origin/master)
The two branches you are trying to merge share no commit, usually because the GitHub repository was created with a README and the local one with git init. If that is intended, merge with git pull origin main --allow-unrelated-histories, then resolve the conflicts.
What this error means
A Git merge relies on a common ancestor (merge base) to know what changed on each side. When the two histories start from different root commits, there is no common ancestor. Since Git 2.9, git merge and git pull refuse this kind of merge by default, because it is often a sign of a mistake: wrong remote, recreated .git folder, or wrong branch. The --allow-unrelated-histories option lifts that safeguard when you know that joining the two histories is really what you want.
Common causes
- Remote repository initialised with a README, licence or
.gitignoreon GitHub/GitLab, then a local project created separately withgit init. - Wrong remote:
originpoints to a different project than you think. .gitfolder deleted and recreated: the new local history starts from scratch and no longer shares anything with the remote.- Intentional merge of two projects (importing a repository into a monorepo): the option is legitimate here.
Step-by-step fix
-
Check the remote
Make sure
originis the right repository and look at its history. If the URL is wrong, fix it instead of forcing the merge.git remote -v git fetch origin git log --oneline origin/main -n 10 # wrong URL? git remote set-url origin [email protected]:user/repo.git -
Merge allowing unrelated histories
If both histories really need to be joined, run the pull again with the option. Git creates a merge commit that links the two roots.
git pull origin main --allow-unrelated-histories -
Resolve conflicts
Files present on both sides (often
README.md,.gitignore,LICENSE) conflict as "both added". Keep the content you want, then finish the merge.git status # edit README.md / .gitignore, then: git add README.md .gitignore git commit -
Push and track the branch
Push the result and link your local branch to the remote one for future
git pullandgit push.git push -u origin main -
Alternative: the remote only has a useless README
If the remote repository was just created and you do not care about its auto-generated README, you can replace it with your history using
--force-with-lease. This permanently removes the remote commits: never do it on a repository where others have already worked.git push --force-with-lease origin main
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- To push an existing local project, create the GitHub repository empty (no README, licence or
.gitignore). - If the remote repository already exists, start with
git cloneand copy your files into it, instead ofgit init. - Never delete the
.gitfolder to "start clean": usegit resetor a new branch.
Frequently asked questions
Is --allow-unrelated-histories dangerous?
It deletes nothing: it only allows a merge Git refuses by default. The risk is joining two unrelated projects if origin is the wrong repository, which is why you should check git remote -v first.
How do I do it in GitHub Desktop or VS Code?
These tools do not expose the option. Open the integrated terminal in the project folder and run git pull origin main --allow-unrelated-histories, then go back to the UI to resolve the conflicts.
Why does GitHub say "entirely different commit histories" on my pull request?
The two branches of the PR share no ancestor. Merge locally first with --allow-unrelated-histories, push the branch, and the comparison works again.
Useful tools for this error
More Git & GitHub errors
- Fix "failed to push some refs" (! [rejected] fetch first) in Git
- Fix "Permission denied (publickey)" with Git and GitHub
- Get out of "detached HEAD" state in Git without losing commits
- Fix "Your local changes to the following files would be overwritten by merge" in Git
- Resolve "CONFLICT (content): Merge conflict" and "Automatic merge failed" in Git
- Fix "Support for password authentication was removed" on GitHub
- Fix "GH001: Large files detected" (file over 100 MB) on GitHub