fatal: refusing to merge unrelated histories
Git won’t merge two branches that don’t share a common ancestor. This usually happens when you created a repo on GitHub with a README and then try to pull into a local repo that was initialized separately.
Fix: Allow unrelated histories
git pull origin main --allow-unrelated-histories
That’s it. Git will merge the two histories. You might get merge conflicts — resolve them normally.
For rebase:
git rebase origin/main --allow-unrelated-histories
When this happens
- You ran
git initlocally AND created the repo on GitHub with a README/license - You’re combining two separate repos into one
- You changed the remote to a different repository
Prevention
When creating a new project, pick one:
Option A: Create on GitHub first, then clone:
# Create repo on GitHub (with README), then:
git clone https://github.com/user/repo.git
Option B: Create locally first, push to empty GitHub repo:
git init
git add -A
git commit -m "Initial commit"
git remote add origin https://github.com/user/repo.git
git push -u origin main
Don’t do both (init locally AND create GitHub repo with files).
See also: Git cheat sheet | Git push rejected fix