Skip to main content

Git · 4 common causes

fatal: refusing to merge unrelated histories

Updated

The two branches share no common ancestor, so Git will not merge them unless you say explicitly that you mean to.

The exact message

$ git pull origin main
From github.com:you/project
 * branch            main       -> FETCH_HEAD
fatal: refusing to merge unrelated histories

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is fatal: refusing to merge unrelated histories.

How to fix it

Cause 1 of 4

You created the repository locally and also initialised one on the host, so each has its own root commit.

Look at both histories first — git log --oneline HEAD and git log --oneline origin/main — so you know what you are joining. Then merge them deliberately: git pull origin main --allow-unrelated-histories. Expect conflicts in files that exist on both sides, such as README and .gitignore, and resolve them before committing.

Cause 2 of 4

The remote history was rewritten — squashed, filtered, or force-pushed over.

Confirm which side is authoritative before joining anything. If the remote is correct and your local commits are the strays, the cleanest route is a fresh git clone into a new directory, then copy your changed files across and commit them normally. Nothing is lost, because your old clone is still on disk.

Cause 3 of 4

The remote URL points at a different repository than you think.

git remote -v. Pasting the wrong repository URL produces exactly this error, since the two projects genuinely are unrelated. Correct it with git remote set-url origin <url>.

Cause 4 of 4

You ran git init inside a directory that was already meant to be a clone.

Check for the extra root commit with git log --oneline | tail -5. If the local repository holds nothing you need, move it aside — rename the folder rather than deleting it — and clone the remote fresh.

Stopping it coming back

Create the repository in one place only: either clone the empty remote before your first commit, or create the remote from your existing local repository and push. Do not initialise both.

fatal: refusing to merge unrelated histories — common questions

Is --allow-unrelated-histories dangerous?
It is not destructive — it creates a merge commit with two parents and nothing is deleted. What it does do is permanently join two unrelated trees, so check that the remote is the repository you meant before running it.
Why did this only start in newer Git versions?
Git 2.9 made refusing the default, because silently merging unrelated histories was almost always a mistake — usually a wrong remote URL. The flag restores the old behaviour explicitly.
The merge left duplicate files. What now?
That is the expected shape of the conflict: both roots contributed a README or a .gitignore. Resolve each conflicted file into the version you want, git add it, and commit the merge. git status lists exactly which files are waiting.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor