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 historiesCommon 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
- Pass the override flag only when you genuinely want to join the two histories.
- Resolve any conflicts that result.
Terminal
git pull origin main --allow-unrelated-historiesAvoid the divergence instead
- Clone the existing repo rather than re-running git init in CI.
- Reset the branch onto the remote tip when a clean base is acceptable.
Terminal
git fetch origin
git reset --hard origin/mainHow 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
Git "Could not find remote branch" in CIFix the Git "fatal: Remote branch <name> not found in upstream origin" error in CI when a clone or checkout t…
Git "shallow update not allowed" in CIFix the Git "shallow update not allowed" push error in CI, which happens when you try to push from a shallow…
Git "not a git repository" in CIFix the Git "fatal: not a git repository (or any of the parent directories): .git" error in CI when a step ru…