Skip to content
Latchkey

Git "fatal: refusing to merge unrelated histories" in CI

Git refused to merge two branches that share no common commit. This is a safety check - it usually means the local history was created independently of the remote (a fresh git init, a re-created repo, or an over-shallow clone), not that a real merge was intended.

What this error means

A git pull or git merge fails with fatal: refusing to merge unrelated histories. It commonly appears when a job initializes a new repo and then pulls from an existing remote, or after a force-recreated repository.

git pull output
fatal: refusing to merge unrelated histories

Common causes

The two branches have no common ancestor

A locally git init-ed repo and the remote were created separately, so they share no root commit. Git will not merge them by default.

A shallow clone hides the common ancestor

If the shared base commit was not fetched, Git cannot see the relationship and treats the histories as unrelated.

How to fix it

Confirm the histories really should merge

Check whether the local repo was created independently. If it was a mistake (a stray git init), re-clone the remote instead of merging.

Terminal
git log --oneline -1
git remote -v
# if the local repo was an accidental init, re-clone:
rm -rf repo && git clone https://github.com/org/repo.git

Allow the merge intentionally

When you genuinely want to join two independent histories (e.g. importing a project), pass the explicit flag.

Terminal
git pull origin main --allow-unrelated-histories
# or
git merge other-branch --allow-unrelated-histories

How to prevent it

  • Clone the remote instead of git init + git pull in CI scripts.
  • Fetch enough history (fetch-depth: 0) when a merge needs the common ancestor.
  • Reserve --allow-unrelated-histories for deliberate project imports.

Frequently asked questions

What causes ""refusing to merge unrelated histories""?
A locally git init-ed repo and the remote were created separately, so they share no root commit. Git will not merge them by default.
How do I fix "refusing to merge unrelated histories"?
Check whether the local repo was created independently. If it was a mistake (a stray git init), re-clone the remote instead of merging.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card