Git: "Refusing to Merge Unrelated Histories"

The two branches share no common ancestor — GitHub-created repos with a seed commit vs your local repo, or a repo re-initialized from scratch. The escape hatch exists, but understand what you're joining first.

What you'll see

Root causes

Remote got seeded commits you don't have

Creating a GitHub repo with 'Add a README' makes an initial commit your local repo never saw. Different roots = unrelated. The fix is one pull flag — or don't seed the remote next time.

History was re-created from scratch

Re-init (rm -rf .git; git init), a filtered/rewritten history, or importing from a tarball: the new root has no ancestor of the old one.

Fix it

  1. See the two roots to confirm they're really unrelated
    git log --oneline origin/main | tail -3; git log --oneline main | tail -3   # different root commits
  2. Merge with the explicit override
    git pull origin main --allow-unrelated-histories   # resolve conflicts (likely the README), commit
  3. Or take theirs wholesale where it's just the seed files
    git merge origin/main --allow-unrelated-histories -X ours   # keep your versions on conflicts
  4. Prevent it next time: create remotes empty
    # GitHub: leave 'Add a README' unchecked when you'll push an existing local repo

Field note

--allow-unrelated-histories doesn't rewrite anything — it just permits the merge. The result is one repo containing both root commits. If the 'unrelated' side has real divergent code you care about, review what you're merging: the flag bypasses a safety check, not a bug.

Common questions

Is --allow-unrelated-histories safe?

Yes — it merges both histories with both root commits preserved. It's refused by default because it usually indicates a mistake (wrong remote). Verify the remote is the right project first.

Should I instead force-push my local history over the remote?

Only if the remote truly has nothing worth keeping (a lone seeded README). Force-push replaces the remote's commits — on shared repos, that deletes other people's history. When in doubt, merge instead.

Ship it right the first time

Our most-documented failures, packaged as ready-to-ship starter kits: Docker, Kubernetes, and Terraform.

Browse the template store →

One-time. Yours to modify. Instant download from the NinjaOps template store.

Get new fixes by email

One short email when new fixes and production templates drop. No spam, unsubscribe anytime.

Partner pick — sponsored

Vultr — our lab-environment pick for this stack

Spin up a cloud server in 60 seconds and reproduce this fix yourself — pay by the hour.

Get Vultr →
Also vetted

Sentry — Free tier: see the exact line of code that broke, before users report it.

Get Sentry →

We earn a commission if you buy through our links — it never costs you extra. More vetted tools on our picks hub · comparing clouds? DigitalOcean vs Vultr and vs AWS · full deals: DigitalOcean · Vultr · NordLayer · Semrush