Skip to content
Latchkey

Git "refusing to merge unrelated histories" in CI

Git found no common ancestor between the two histories and refuses to merge by default, a safety check against accidentally combining unrelated repositories.

What this error means

A git merge or git pull aborts with fatal: refusing to merge unrelated histories. It commonly appears when a CI step pulls into a freshly initialized or re-created repo.

git
fatal: refusing to merge unrelated histories

Common causes

Histories were never connected

One side was re-initialized with git init or imported separately, so the two branches share no base commit.

Force-pushed or rewritten history

A rewrite orphaned the previous history, leaving the remote and local with disjoint roots.

How to fix it

Allow the merge explicitly

  1. Pass the override flag only when you genuinely want to join the two histories.
  2. Resolve any conflicts that result.
Terminal
git pull origin main --allow-unrelated-histories

Avoid the divergence instead

  1. Clone the existing repo rather than re-running git init in CI.
  2. Reset the branch onto the remote tip when a clean base is acceptable.
Terminal
git fetch origin
git reset --hard origin/main

How to prevent it

  • Do not git init over an existing repo in CI; clone it. Reserve --allow-unrelated-histories for deliberate one-time merges of genuinely separate projects.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →