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
- Created a GitHub repo with README, then pushed a local repo β two separate initial commits
- Merging two completely different repositories
- 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 -vto verify youβre pushing to the correct remote before force-pushing.
Related: How Git Tracks Changes Β· Git Push Rejected fix