Skip to content

git rebase: cannot rebase onto unrelated history

Quick summary

You tried to rebase onto a branch that shares no commit history with your current branch — git blocks this to prevent losing commits.

Why this happens

  • The two branches were created independently with git init and never shared a common ancestor commit

  • You are rebasing across two completely separate repositories that were merged without --allow-unrelated-histories

  • A shallow clone truncated the common ancestor, making git think the histories are unrelated

Minimal example

✗ Broken code
git rebase main  # fatal: refusing to merge unrelated histories
✓ Fixed code
git rebase --allow-unrelated-histories main  # forces rebase across independent roots

The --allow-unrelated-histories flag tells git you intentionally want to combine branches with no common ancestor.

How to diagnose

  • Run git merge-base <branch1> <branch2> — if it returns nothing, the branches share no common ancestor

  • Check git log --oneline --graph for both branches to confirm they have independent root commits

  • Verify whether the repository was created fresh or shallow-cloned without full history

How to fix

  • Use git rebase --allow-unrelated-histories to force the rebase when you are certain the histories should be joined

  • Merge instead of rebase with git merge --allow-unrelated-histories to combine the two independent histories

  • Re-clone with full depth using git clone --unshallow if a shallow clone caused the false unrelated detection

How to prevent

  • Always branch from an existing commit rather than running git init separately for related work

  • Avoid shallow clones when you need to rebase across branches that may share distant ancestors

Related Resources

Related Glossary

Related Lessons

← Back to language