πŸ”§ Error Fixes
Β· 1 min read

Git: Refusing to Merge Unrelated Histories β€” How to Fix It


fatal: refusing to merge unrelated histories means you’re trying to merge two branches that have no common commit ancestor β€” they’re completely separate histories.

Why this happens

Git requires a common ancestor commit to perform a three-way merge. When two repositories were initialized independently (each with their own first commit), Git has no shared base to compare changes against. This most commonly occurs when you create a GitHub repo with a README and then try to push a local repo that was initialized separately.

What causes this error

  1. Created a GitHub repo with README, then pushed a local repo β€” two separate initial commits
  2. Merging two completely different repositories
  3. Rebased and lost the common ancestor

Fix 1: Allow unrelated histories

git pull origin main --allow-unrelated-histories
# Resolve any merge conflicts, then commit

Fix 2: Start fresh (if the remote only has a README)

# If the remote just has a README/LICENSE you don't need:
git push origin main --force
# ⚠️ This overwrites the remote β€” only do this if nothing important is there

Alternative solutions

Rebase instead of merge:

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

Delete and recreate the remote repo empty, then push your local repo without any flags. This avoids the problem entirely.

Fix 3: Prevent it next time

When creating a GitHub repo, uncheck β€œInitialize this repository with a README” if you plan to push an existing local repo. Create the repo empty, then push.

When NOT to force

If both histories have real work in them, use --allow-unrelated-histories and carefully resolve conflicts. Don’t force-push β€” you’ll lose the remote work.

Prevention

  • Always create remote repos without initialization (no README, .gitignore, or license) when you already have a local repo to push.
  • Use git remote -v to verify you’re pushing to the correct remote before force-pushing.

Related: How Git Tracks Changes Β· Git Push Rejected fix

πŸ“˜