Skip to content
fatal: refusing to merge unrelated histories

fatal: refusing to merge unrelated histories

DodaTech 3 min read

Git’s fatal: refusing to merge unrelated histories error happens when you try to merge two branches (or repos) that have no common ancestor commit, and Git blocks the merge to prevent an accidental integration of completely separate projects.

What It Means

Git tracks history as a chain of commits. When you merge branch A into branch B, Git expects them to share at least one common commit — their last common ancestor. If they don’t share any commit history, Git refuses to merge because it can’t determine how to reconcile the two divergent histories.

Why It Happens

  • You created a new repo with git init, made commits, then tried to git pull from an existing remote repo — your local history and the remote history share no common commits.
  • You’re merging two branches that were created independently and never shared a base.
  • You used git checkout --orphan to create an orphan branch with no parent, then tried to merge it.
  • You’re combining two separate repositories that were never linked.

How to Fix It

1. Allow unrelated histories during merge

Pass the --allow-unrelated-histories flag to override Git’s safety check:

git merge --allow-unrelated-histories <branch-name>

Or during a pull:

git pull origin main --allow-unrelated-histories

2. Handle merge conflicts

Since the two histories have no common base, every file difference will appear as a conflict. Resolve them manually or accept all incoming/current changes:

# Accept all incoming changes
git merge --allow-unrelated-histories <branch-name> -X theirs

# Or accept all current branch changes
git merge --allow-unrelated-histories <branch-name> -X ours

3. Rebase instead of merge (alternative)

If you want a linear history, fetch and rebase with the same flag:

git fetch origin
git rebase --allow-unrelated-histories origin/main

4. Verify the result

git log --oneline --graph --all

This shows the combined commit graph.

Is --allow-unrelated-histories safe?
It’s safe if you intend to combine two independent projects or histories. However, it bypasses Git’s conflict-detection mechanism at the history level, which often leads to numerous merge conflicts in the working tree. Always review the diff before committing the merge with git diff --cached.
Why does Git block unrelated history merges by default?
Git assumes that two branches with no common ancestor are separate projects that shouldn’t be merged. If Git allowed this silently, you could accidentally merge two unrelated repositories (e.g., your blog repo into your API server repo) without warning. The flag exists for intentional use cases like combining repos or adopting an external codebase.
Does this happen with git pull too?
Yes. If your local repo was initialized with git init and has commits, and you then add a remote and run git pull, Git sees no shared history between your local commits and the remote commits. The fix is the same: git pull origin main --allow-unrelated-histories.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro