How to fix "fatal: refusing to merge unrelated histories"
Tested: we reproduced this error and confirmed the fix on Ubuntu 24.04 with Git 2.43.0, November 29, 2026.
The two branches you’re joining have no commit in common, so Git refuses to guess how they fit together. If they’re meant to be one project, tell Git that’s fine:
git pull --no-rebase --allow-unrelated-histories origin main
git push origin main
In our test this merged a local project with a remote that had its own “Initial commit” (a README created on the website), and the push that followed went through.
Why it happens
You get this most often when you:
- create a repo on GitHub or GitLab with “Add a README” ticked, then try to push a project that already has its own commits
- point
originat a different repository by mistake - rewrite history on one side (a squash of everything, or a new
git init) and then pull the old copy
Check which one it is before merging:
git remote -v
git log --oneline --graph --all -n 20
If the remote URL is the one you expect and the graph shows two lines that never join, it’s the first case and the fix above is right. If the URL is wrong, fix it with git remote set-url origin <url> instead.
What we saw in Git 2.43
We created a bare remote with one commit and a local repo with a different first commit, then tried each command:
| Command | Result |
|---|---|
git pull origin main (no pull setting) |
fatal: Need to specify how to reconcile divergent branches. |
git pull --no-rebase origin main |
fatal: refusing to merge unrelated histories |
git fetch then git merge origin/main |
fatal: refusing to merge unrelated histories |
git push origin main |
Rejected: the tip of your current branch is behind |
git pull --no-rebase --allow-unrelated-histories origin main |
Merged, then push worked |
git fetch then git merge origin/main --allow-unrelated-histories |
Merged (same result, in two steps) |
git pull --rebase origin main |
Worked: local commits replayed on top of the remote’s |
Option 2: rebase instead of merging
If you’d rather keep a straight history without a merge commit, rebase your local commits onto the remote:
git pull --rebase origin main
git push origin main
This worked in our test without the --allow-unrelated-histories flag. If both sides changed the same file (for example both have a README), you’ll get a conflict to resolve first.
Combining two separate repositories on purpose
The same flag joins two projects that were never related, for example folding an old repo into a new one while keeping both histories:
git remote add old ../old-project
git fetch old
git merge old/main --allow-unrelated-histories
git remote remove old
If both projects have files with the same names (README.md, package.json), expect conflicts. Moving the old project into a subfolder in its own repo first (a commit that runs git mv on everything) keeps the two sets of files apart.
If the files conflict
A merge stops with CONFLICT (add/add) when both histories added the same file. Open it, keep what you want, then:
git add README.md
git commit
git push origin main
Confirm it worked
git log --oneline --graph -n 10 should now show both first commits joined by a merge commit (or, after a rebase, your commits on top of the remote’s). git status should say your branch is up to date with origin/main.
FAQ
Is --allow-unrelated-histories safe?
Yes, when the two histories really belong together, such as your local project and a new GitHub repo with only a README. It creates a normal merge commit. Don't use it if the remote is a different project; check git remote -v first.
How do I avoid this on a new GitHub repo?
Create the repo empty (no README, license or .gitignore) and push your existing code to it. Or clone the new repo first and copy your files into it.
Why didn't git pull show this error?
Recent Git versions stop earlier, with fatal: Need to specify how to reconcile divergent branches, if you haven't set pull.rebase. Run git pull --no-rebase to see the unrelated histories error, or pick a strategy as shown on this page.
