fatal: refusing to merge unrelated histories — how to fix
fatal: refusing to merge unrelated histories
Quick fix: Run git pull origin main --allow-unrelated-histories, resolve any conflicts, then commit.
What it means
Git found that the two branches you are combining have no commit in common. They are two separate projects as far as Git is concerned, so since Git 2.9 it refuses to merge them unless you say that's what you want.
Why it happens
- You ran
git initlocally, made commits, then added a remote that already has commits (often a README, LICENSE or.gitignorecreated on GitHub/GitLab when you made the repository). - You re-created the
.gitfolder, so your local history no longer matches the remote. - You are combining two different repositories on purpose.
Fix 1: allow the merge (most common)
git pull origin main --allow-unrelated-histories
# resolve conflicts if any (often README.md), then:
git add .
git commit -m "Merge remote history"
git push origin main
Replace main with your branch name (master on older repos).
Fix 2: rebase your commits on top of the remote
This keeps a straight history without a merge commit:
git fetch origin
git rebase origin/main
git push origin main
If Git still complains, git rebase --root --onto origin/main replays every local commit, including the first one, on top of the remote.
Fix 3: the remote content doesn't matter
If the remote only has a generated README you don't need, overwrite it. This deletes the remote commits:
git push --force origin main
How to avoid it
When creating a repository on GitHub for an existing local project, leave "Add a README", ".gitignore" and "license" unchecked, then push your local repository into the empty remote.
Last updated 2026-10-27.