Skip to content

Refusing 到合并 Unrelated Histories

错误信息

                fatal: refusing 到合并 unrelated histories
              

快速摘要

你是 trying 到合并 two 仓库那个是创建 independently and share 否常见 ancestor.

为什么发生

  • 你 initialized 新仓库 (git init) in directory 那个 already 有 remote 使用 history

  • 你是合并分支从 completely separate 仓库

  • 仓库是 re-创建从 scratch 相反的 cloned

最小示例

✗ 错误代码
git init
git remote add origin https://github.com/user/repo.git
git pull origin main  # refusing to merge unrelated histories
✓ 修复代码
# Option 1: allow the merge
git pull origin main --allow-unrelated-histories

# Option 2: re-clone correctly
git clone https://github.com/user/repo.git

如果你 git init 新仓库 and 然后拉取从 remote 使用 existing history, histories 是 unrelated. Use --允许-unrelated-histories 如果 intentional, or re-克隆.

如何诊断

  • 检查 git log on 两者都分支到 see 如果它们 share 任何常见提交

  • 验证你 cloned 仓库相反的 initializing 新 one

  • 检查如果 remote URL 是正确

如何修复

  • 如果你 intentionally 想要到合并 unrelated histories: git 拉取原点主要 --允许-unrelated-histories

  • Re-克隆仓库正确地相反的 git init + git remote 添加

  • 如果你有局部工作到 preserve, 创建 patch and apply 它到 fresh 克隆

如何预防

  • Use git 克隆到复制 existing 仓库, 不 git init + git remote 添加

  • 验证 remote URL 之前 pulling

相关资源

相关术语

相关课程

← 返回语言