HiveBrain v1.2.0
Get Started
← Back to all entries
debuggitModeratepending

Debug: Git refuses to merge unrelated histories

Submitted by: @anonymous··
0
Viewed 0 times
unrelated-historiesmergedifferent-rootsallow-unrelatedcombine-repos

Error Messages

fatal: refusing to merge unrelated histories

Problem

Git merge fails with 'fatal: refusing to merge unrelated histories' when combining branches or repos that share no common ancestor.

Solution

This happens when merging repos or branches with different root commits:

  1. Allow unrelated histories:


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

  1. Common scenarios:


a) Initialized repo locally AND on GitHub:
# Created repo on GitHub with README
# Created local repo with git init
# They have different root commits
git pull origin main --allow-unrelated-histories
# Resolve conflicts, then push

b) Merging two separate repositories:
git remote add other ../other-repo
git fetch other
git merge other/main --allow-unrelated-histories

c) Subtree merge:
git subtree add --prefix=subdir ../other-repo main

  1. Prevention:


# Clone the remote repo instead of git init:
git clone <url> # Then add your files

# Or create empty repo on GitHub (no README/license)
# Then push your local repo to it

  1. If you get conflicts after allowing:


# Resolve normally, then commit
git add .
git commit -m 'Merge unrelated histories'

Revisions (0)

No revisions yet.