debuggitModeratepending
Debug: Git refuses to merge unrelated histories
Viewed 0 times
unrelated-historiesmergedifferent-rootsallow-unrelatedcombine-repos
Error Messages
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:
git merge main --allow-unrelated-histories
git pull origin main --allow-unrelated-histories
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
# 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
# Resolve normally, then commit
git add .
git commit -m 'Merge unrelated histories'
- Allow unrelated histories:
git merge main --allow-unrelated-histories
git pull origin main --allow-unrelated-histories
- 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
- 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
- If you get conflicts after allowing:
# Resolve normally, then commit
git add .
git commit -m 'Merge unrelated histories'
Revisions (0)
No revisions yet.