Git shows fatal: refusing to merge unrelated histories when you try to merge or pull a branch that has no commit in common with yours. It almost always happens when you create a project locally with git init, create a repository on GitHub with a README or licence, and then try to pull one into the other. The two histories started separately, so Git stops to protect you. If you’re sure they belong together, the fix is git pull origin main --allow-unrelated-histories, then resolve any conflicts and commit.
I reproduced the error in a test repository with Git 2.47.3 on 12 October 2026, using a local “remote” in place of GitHub so every step is visible. All output below is real.
What “unrelated histories” means
Every Git repository is a chain of commits, each pointing to its parent. When you merge, Git looks for the merge base: the most recent commit both branches share. It compares each side against that base to work out what changed.
When two repositories were created separately, each has its own first commit (a “root” commit) and there is no shared ancestor. You can check this directly with git merge-base:
$ git fetch origin
$ git merge-base HEAD origin/main
$ echo $?
1
No commit printed, and exit code 1: there’s no merge base. Without one, a merge would just glue two unrelated projects together, which is rarely what you meant.
Seeing the two root commits
A root commit is a commit with no parent. A normal repository has one. After merging unrelated histories, it has two, and you can list them:
$ git rev-list --max-parents=0 HEAD
afe6969
18c401d
$ git log --oneline --max-parents=0
afe6969 First local commit
18c401d Initial commit
That’s a handy way to confirm what happened, and to spot repositories where unrelated histories were merged in the past.
Why Git refuses by default
This wasn’t always an error. The Git 2.9 release notes explain that merging unrelated histories used to be allowed, and that this let “an unnecessary parallel history” get merged into projects by unsuspecting maintainers. From Git 2.9 onwards, git merge refuses by default, with an escape hatch: the --allow-unrelated-histories option.
The official git merge documentation adds that, because merging independent projects is “a very rare occasion, no configuration variable to enable this by default exists or will be added.” You have to opt in each time, on purpose.
Git terms used in this post
- Commit: a saved snapshot of your files, with a message and a pointer to its parent commit.
- Root commit: the first commit in a history, with no parent.
- Merge base: the most recent commit two branches have in common.
- Remote: a copy of the repository somewhere else, such as GitHub.
origin is the default name for it.
- Merge: combining two branches, usually with a new “merge commit” that has two parents.
- Rebase: replaying your commits on top of another branch, giving a straight line of history.
Two histories are “unrelated” when they have different root commits and no merge base.
How to reproduce the error
Here’s the common beginner scenario, step by step:
- Create a repository on GitHub and tick “Add a README file”. GitHub makes an “Initial commit” on
main.
- On your computer, run
git init, add your project files and commit them.
- Add the GitHub repository as
origin and try to pull.
In my test, the local repository had one commit (“First local commit” with index.php and a README.md), and the remote had its own “Initial commit” with a README.md and LICENSE. With a recent Git, the first git pull doesn’t even get as far as the unrelated-histories check; it asks how to reconcile the branches:
$ git pull origin main
hint: You have divergent branches and need to specify how to reconcile them.
...
fatal: Need to specify how to reconcile divergent branches.
Choosing merge (--no-rebase), or merging the fetched branch directly, produces the error this post is about:
$ git pull --no-rebase origin main
From ../remote
* branch main -> FETCH_HEAD
* [new branch] main -> origin/main
fatal: refusing to merge unrelated histories
$ git merge origin/main
fatal: refusing to merge unrelated histories
If you skip pulling and try to push first, you get a different message that sends you back to pull:
error: failed to push some refs to '../remote.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
Fix 1: merge with --allow-unrelated-histories
If both histories genuinely belong to the same project, tell Git so:
$ git pull --no-rebase origin main --allow-unrelated-histories
From ../remote
* branch main -> FETCH_HEAD
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
The --no-rebase makes the pull a merge, so it works whatever your pull.rebase setting is. (git pull passes --allow-unrelated-histories through to git merge; the Git 2.9 notes mention that too.) If you’ve already fetched, git merge origin/main --allow-unrelated-histories does the same thing.
Resolving the add/add conflict
Both sides created a file called README.md, so Git reports an add/add conflict. git status --short shows the state:
A LICENSE
AA README.md
LICENSE only existed on the remote, so it was added cleanly (A). README.md was added on both sides (AA) and contains conflict markers. Open it, keep what you want, delete the <<<<<<<, ======= and >>>>>>> lines, then stage and commit:
$ git add README.md
$ git commit -m "Merge remote main with unrelated history"
$ git log --oneline --graph
* 3c08e61 Merge remote main with unrelated history
|\
| * 18c401d Initial commit
* afe6969 First local commit
The graph shows two root commits joined by a merge commit. From now on, the histories are related, and normal git pull and git push work. In my test, git push origin main succeeded straight after.
Changed your mind? Abort the merge
If the conflicts look wrong, or you realise you’re merging the wrong repository, stop before committing:
$ git status --short
A LICENSE
AA README.md
A index.php
$ git merge --abort
$ git status --short
$ git log --oneline
da4278f a
git merge --abort returned my test repository to exactly the state before the merge: a clean working tree and only the original commit.
Fix 2: rebase your commits on top instead
If you prefer a straight-line history without a merge commit, rebase your local commits onto the remote branch:
$ git pull --rebase origin main
Successfully rebased and updated refs/heads/main.
In a second test repository with one local commit, this replayed the commit on top of the remote history without any unrelated-histories error, because rebasing doesn’t need a merge base in the same way. If both sides touch the same file, you’ll resolve conflicts during the rebase instead. Rebasing rewrites your local commits, so only do it with commits you haven’t shared yet.
Fix 3: start from a clone instead
For a brand-new project, the simplest fix is to avoid the problem:
- Clone the GitHub repository first:
git clone <url>.
- Copy your project files into the cloned folder.
- Commit and push.
In my test, a fresh clone contained the merged history and all three files (LICENSE, README.md, index.php), with no special options needed. If you haven’t made any commits locally that you care about, this is the cleanest route.
How to avoid the error next time
- Create the GitHub repository empty. Don’t tick “Add a README”,
.gitignore or licence if you already have a local project. GitHub then shows push instructions and there’s only one history.
- Or clone first, then add files. As in Fix 3.
- Check the remote URL.
git remote -v shows where origin points. Pulling from the wrong repository is the one case where this error is saving you from a real mistake.
The right way to connect a local project to a new GitHub repository
To never see this error, create the GitHub repository empty and push your existing project into it. I tested this sequence against an empty remote:
$ git init
$ git add .
$ git commit -m "First commit"
$ git branch -M main
$ git remote add origin <your-repo-url>
$ git push -u origin main
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
git branch -M main renames your branch to main in case your Git defaults to master. -u sets up tracking, so afterwards a plain git pull and git push know where to go; git status -sb showed ## main...origin/main. If you’re also adding an AGENTS.md for AI coding tools, my guide on what AGENTS.md is explains what to put in it before that first commit.
When NOT to use --allow-unrelated-histories
Git refuses for a reason. Think twice if:
- You’ve pointed
origin at the wrong repository. Merging would mix two different projects. Fix the URL with git remote set-url origin <correct-url> instead.
- You re-created a repository by deleting
.git and running git init again. The new history is unrelated to the old remote. It’s usually better to clone the remote again and copy your changes in.
- You’re on the wrong branch. Branches in the same repository normally share history. An unrelated-histories error between them suggests an orphan branch, such as a
gh-pages branch, which usually shouldn’t be merged into main.
If you merge by mistake and haven’t pushed yet, my guide to undoing the last commit and keeping your changes explains how to step back safely.
Merging two separate projects on purpose
Occasionally you really do want to combine independent repositories, for example moving two small projects into one monorepo while keeping their history. The usual approach is:
- Add the other repository as a second remote:
git remote add other <url>.
- Fetch it:
git fetch other.
- Merge with the flag:
git merge other/main --allow-unrelated-histories.
- Resolve conflicts, typically by moving one project’s files into a sub-folder first.
This is the “two projects that started their lives independently” case the documentation mentions, and it’s what the option exists for.
What your history looks like afterwards
Once you’ve merged with --allow-unrelated-histories, the merge is permanent history like any other. Everyone who clones the repository gets both root commits and the merge commit joining them. That’s normally harmless, and git log simply shows a little extra history at the start.
Two things are worth knowing. First, tools that walk back to “the first commit” (for example, scripts using git rev-list --max-parents=0 HEAD) will find two. Second, if you merged by mistake and already pushed, undoing it means rewriting shared history, which affects everyone else using the repository. It’s far easier to check git remote -v and the files involved before you merge than to clean up afterwards.
Related errors you might see
fatal: Need to specify how to reconcile divergent branches. Not the same error, but it often comes first. Choose --no-rebase (merge), --rebase or --ff-only, or set a default with git config pull.rebase false.
Updates were rejected because the remote contains work that you do not have locally. Your push was refused because the remote has commits you don’t. Pull first, using one of the fixes above.
- Conflicts with uncommitted work. If Git won’t pull because of local changes, stash them first; my post on git stash pop vs apply explains how to get them back afterwards.
FAQ
What does “fatal: refusing to merge unrelated histories” mean?
It means the branch you’re merging or pulling has no commit in common with your current branch. Since Git 2.9, Git refuses such merges by default to stop unrelated projects being combined by accident.
How do I fix refusing to merge unrelated histories?
If the histories belong together, run git pull origin main --allow-unrelated-histories (add --no-rebase if Git asks how to reconcile branches). Resolve any conflicts, then git add and git commit.
Why does this happen after creating a GitHub repository?
Ticking “Add a README” makes GitHub create its own first commit. Your local git init project has a separate first commit, so the two histories are unrelated. Creating the GitHub repository empty avoids it.
Is --allow-unrelated-histories safe?
It’s safe when both histories are really the same project, such as a local project and a new GitHub repository. It’s risky if origin points at the wrong repository, because you’d merge two different projects.
Can I make --allow-unrelated-histories the default?
No. The Git documentation says no configuration variable exists or will be added for it, because merging unrelated histories should be rare and deliberate.
What is an add/add conflict?
It happens when both branches added a file with the same path, such as README.md. Edit the file to keep the content you want, remove the conflict markers, then stage and commit it.
Should I use rebase instead of --allow-unrelated-histories?
You can. git pull --rebase origin main replays your local commits on top of the remote ones and gives a straight-line history. Only rebase commits you haven’t pushed or shared yet.