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.
fatal: refusing to merge unrelated historiesCommon 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.
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.gitAllow the merge intentionally
When you genuinely want to join two independent histories (e.g. importing a project), pass the explicit flag.
git pull origin main --allow-unrelated-histories
# or
git merge other-branch --allow-unrelated-historiesHow to prevent it
- Clone the remote instead of
git init+git pullin CI scripts. - Fetch enough history (
fetch-depth: 0) when a merge needs the common ancestor. - Reserve
--allow-unrelated-historiesfor deliberate project imports.