How to fix fatal: refusing to merge unrelated histories in Git

GitBEGINNERMEDIUMUpdated Dec 6, 2025ยท 236 views

This error occurs when Git detects that two branches or repositories have no common commit history. Git refuses the merge by default to prevent accidentally combining unrelated projects.

Quick fix

Fixes fatal: refusing to merge unrelated histories

git pull origin main --allow-unrelated-histories
  1. Use --allow-unrelated-histories flag with git pull
  2. Use --allow-unrelated-histories flag with git merge
  3. Resolve any merge conflicts
  4. For shallow clones, fetch full history
git โ€” error
git pull origin main --allow-unrelated-histories
fatal: refusing to merge unrelated histories

What this error means

When you see the error "fatal: refusing to merge unrelated histories", Git is telling you that it cannot find a common ancestor commit between the branches you are trying to merge. This safety mechanism was introduced in Git 2.9 to prevent users from accidentally merging two completely separate projects. In Git, branches are expected to share a common starting point in their history. When two branches have completely independent commit histories with no shared commits, Git considers them "unrelated" and blocks the merge operation to protect against unintended repository corruption or confusion. This commonly happens when you create a new local repository, make some commits, and then try to pull from a remote repository that was initialized separately. It can also occur if the .git directory was corrupted or deleted, breaking the historical connection between your local and remote repositories.

Typical symptoms

  • Git pull or merge commands fail with "fatal: refusing to merge unrelated histories"
  • Attempting to rebase fails with the same error message
  • Cannot integrate a remote repository with your local repository
  • git log shows completely different commit histories between branches
  • The merge operation is blocked even though both repositories contain related code

Common causes

  1. You created a local repository and added commits before connecting it to an existing remote repository that has its own commits
  2. The .git directory was deleted or corrupted, causing Git to lose track of the commit history
  3. You initialized a new repository on GitHub with a README and then tried to push an existing local repository
  4. A shallow clone (using --depth) does not contain enough history to find the common ancestor
  5. You are trying to merge two genuinely unrelated projects that were never meant to share history
  6. Repository was recreated or force-pushed, resulting in a completely new commit history on the remote

How to fix "fatal: refusing to merge unrelated histories"

1Use --allow-unrelated-histories flag with git pull

If you are trying to pull from a remote repository and encounter this error, add the --allow-unrelated-histories flag:

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

Replace main with your target branch name (e.g., master). This tells Git to proceed with the merge even though the branches have no common history. You may need to resolve merge conflicts after running this command.

2Use --allow-unrelated-histories flag with git merge

If you are merging a local branch, use the flag with the merge command:

bash
git merge <branch-name> --allow-unrelated-histories

For example, to merge a branch called feature into your current branch:

bash
git merge feature --allow-unrelated-histories

Git will create a merge commit that combines both unrelated histories into one unified history.

3Resolve any merge conflicts

After allowing unrelated histories, Git will attempt to merge the files. If there are conflicting changes, you need to resolve them:

bash
# Check which files have conflicts
git status

# Open conflicting files and resolve the conflicts manually
# Look for conflict markers: <<<<<<<, =======, >>>>>>>

# After resolving, stage the files
git add <resolved-file>

# Complete the merge
git commit -m "Merge unrelated histories"
4For shallow clones, fetch full history

If the error is caused by a shallow clone that does not have enough history, fetch the complete repository history:

bash
git fetch --unshallow

This converts your shallow clone into a complete clone with full history, which may resolve the unrelated histories error.

5Alternative: Clone fresh and migrate changes

If the above solutions do not work or you want a cleaner approach, you can clone the remote repository fresh and copy your changes:

bash
# Backup your current work
cp -r my-project my-project-backup

# Clone the remote repository
git clone <remote-url> my-project-new

# Copy your changed files to the new clone (excluding .git)
rsync -av --exclude='.git' my-project-backup/ my-project-new/

# Stage and commit your changes
cd my-project-new
git add .
git commit -m "Add local changes"

Advanced notes

Understanding the flag:
The --allow-unrelated-histories flag was introduced in Git 2.9. In earlier versions of Git, merging unrelated histories was allowed by default, which sometimes led to confusion when users accidentally merged unrelated projects.

When NOT to use this flag:
- If you are genuinely working with unrelated projects that should remain separate
- If you suspect your repository was compromised or tampered with
- If you are unsure why the histories are unrelated - investigate first

Prevention strategies:
1. Always clone an existing remote repository before starting work, rather than initializing locally and connecting later
2. When creating a new repository on GitHub, do not initialize with a README if you plan to push an existing local repository
3. Avoid using --depth in clones unless you understand the limitations
4. Keep backups of your .git directory for important repositories

Rebase with unrelated histories:
For rebase operations, you can use:

bash
git rebase <branch> --allow-unrelated-histories

However, be cautious with rebase as it rewrites commit history and can cause issues if others are working on the same branch.

Frequently asked questions

What does "fatal: refusing to merge unrelated histories" mean?
When you see the error "fatal: refusing to merge unrelated histories", Git is telling you that it cannot find a common ancestor commit between the branches you are trying to merge. 9 to prevent users from accidentally merging two completely separate projects. In Git, branches are expected to share a common starting point in their history.
How do I fix "fatal: refusing to merge unrelated histories"?
This error occurs when Git detects that two branches or repositories have no common commit history. Git refuses the merge by default to prevent accidentally combining unrelated projects.
What causes "fatal: refusing to merge unrelated histories"?
You created a local repository and added commits before connecting it to an existing remote repository that has its own commits The .git directory was deleted or corrupted, causing Git to lose track of the commit history You initialized a new repository on GitHub with a README and then tried to push an existing local repository
Is the "fatal: refusing to merge unrelated histories" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading