---
title: "fatal: refusing to merge unrelated histories"
description: "Git refuses to merge two branches that share no commits. Learn why this happens after git init plus a new remote repository, and how to merge safely with --allow-unrelated-histories."
url: https://www.danny.engineering/answer/git/fatal-refusing-to-merge-unrelated-histories
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Git"]
---

# fatal: refusing to merge unrelated histories

## The short answer


Git stops because the two branches have no commit in common, so it has no starting point to merge from. If you are sure they belong together, which is usually a new local project and a freshly created remote that already has a README, tell Git to allow it:


```bash
git pull origin main --allow-unrelated-histories
```


Resolve any conflicts, commit if Git asks you to, then push.


## Why it happens


A normal merge looks for the most recent commit that both branches share, called the merge base, and combines the changes each side has made since then. Two histories that started from different root commits have no merge base at all.


Since Git 2.9, `git merge` and `git pull` refuse this by default, because merging two unrelated histories is usually a mistake, such as pulling from the wrong repository.


The common ways to end up here are:

- You ran `git init` and committed locally, then created a repository on your Git host with a README, licence or `.gitignore`, and tried to pull it. Both sides now have their own first commit.
- You added the wrong remote URL, so you are pulling a different project.
- The remote was recreated, or its history was rewritten from scratch, so its root commit is not the one you have.
- You are deliberately combining two separate projects into one repository.

## How to fix it


### Check you have the right remote first


Before forcing anything, make sure the remote is the project you think it is.


```bash
git remote -v
git fetch origin
git log --oneline origin/main | tail -n 5
git log --oneline main | tail -n 5
```


If `origin` points at the wrong repository, change the URL instead of merging:


```bash
git remote set-url origin git@github.com:your-name/your-repo.git
```


### Merge with --allow-unrelated-histories


This is the usual fix when the remote only holds a starter commit and you want to keep both sides.


```bash
git fetch origin
git merge origin/main --allow-unrelated-histories

# Only if there were conflicts:
git add README.md .gitignore
git commit

git push -u origin main
```


The result is a merge commit with two parents, one from each history.


### Rebase your local commits on top of the remote


If you prefer a straight line of history, replay your local commits on top of the remote's starter commit instead.


```bash
git fetch origin
git rebase origin/main
git push -u origin main
```


This works well when you only have a handful of local commits. If a file such as `README.md` exists on both sides you will need to resolve it, then run `git rebase --continue`.


### Replace the remote (only for a brand new project)


If the remote contains nothing but a generated README you do not want, and nobody else has cloned it, you can overwrite it with your local history.


```bash
git push --force-with-lease origin main
```


This deletes the remote's commits, so only do it on a repository you have just created.


## Common pitfalls

- Adding the flag out of habit. If the histories are unrelated because you pointed at the wrong repository, you will mix two different codebases together.
- Expecting a permanent setting. `--allow-unrelated-histories` is a one-off option for `git merge` and `git pull`; there is no config key for it, and you only need it for the first merge.
- Being surprised by conflicts. Files with the same path on both sides, such as `README.md` or `.gitignore`, show up as add/add conflicts that you resolve by hand.
- Force pushing over a remote that other people have already cloned. Their copies will no longer match.
- Avoiding the problem next time is easy: when you already have a local project, create the remote repository empty, without a README or licence, then push to it.

## Related reading

- [git-merge documentation](https://git-scm.com/docs/git-merge)
- [git-pull documentation](https://git-scm.com/docs/git-pull)
