git "refusing to merge unrelated histories": the squash-imported repo and the flag that fixes it
verbatim errorfatal: refusing to merge unrelated histories
# seen with:
$ git pull origin main
# or a merge into a repo that used to be two projects
Problem
Merging a legacy branch into the restructured monorepo:
fatal: refusing to merge unrelated histories
# seen with:
$ git pull origin main
# or a merge into a repo that used to be two projectsThe branches contain the same files. git refuses to believe they are related, and no amount of fetching helps.
Root cause
Two histories with no common merge-base commit. This is exactly the intended outcome of:
- a repo re-initialized (
.gitdeleted,git initfresh) while keeping old code; - a subtree/subdirectory extracted into its own repo and later merged back;
- a squash-import from another VCS (SVN, Mercurial) whose commit ids share nothing with the git history.
Since git 2.9, merges of unrelated histories require an explicit opt-in — older gits would happily merge and produce more surprises than the refusal does. The refusal is not an error in your repo; it is git declining to guess that two independent timelines should be stitched.
fix preview — first 2 of 3 lines (bash), truncated:
git merge legacy-branch --allow-unrelated-histories
# or:
… 1 more line in the fix🔒 the fix — including 4 code blocks — is members-only. $1/mo unlocks everything.