Skip to content

Refusing to Merge Unrelated Histories

Error message

                fatal: refusing to merge unrelated histories
              

Quick summary

You are trying to merge two repositories that were created independently and share no common ancestor.

Why this happens

  • You initialized a new repo (git init) in a directory that already had a remote with history

  • You are merging branches from completely separate repositories

  • A repository was re-created from scratch instead of cloned

Minimal example

✗ Broken code
git init
git remote add origin https://github.com/user/repo.git
git pull origin main  # refusing to merge unrelated histories
✓ Fixed code
# Option 1: allow the merge
git pull origin main --allow-unrelated-histories

# Option 2: re-clone correctly
git clone https://github.com/user/repo.git

If you git init a new repo and then pull from a remote with existing history, the histories are unrelated. Use --allow-unrelated-histories if intentional, or re-clone.

How to diagnose

  • Check git log on both branches to see if they share any common commits

  • Verify you cloned the repo instead of initializing a new one

  • Check if the remote URL is correct

How to fix

  • If you intentionally want to merge unrelated histories: git pull origin main --allow-unrelated-histories

  • Re-clone the repository correctly instead of git init + git remote add

  • If you have local work to preserve, create a patch and apply it to a fresh clone

How to prevent

  • Use git clone to copy existing repositories, not git init + git remote add

  • Verify the remote URL before pulling

Related Resources

Related Glossary

Related Lessons

← Back to language