如何在内容近似但 commits 不同的 repos 间迁移 branches
结论
即使两个 repository 没有共同 commit,也可以迁移 branch 中的改动。不要修改 origin 后强行执行 pull,也不要合并无关历史。建议按以下步骤处理:
- 使用
git bundle将旧 repository 中的相关 branches 和对象完整带入新环境。 - 确定每个系统专用 branch 对应的旧基线。
- 从旧基线生成完整 tree diff。
- 基于新 repository 的目标基线创建 branch,再应用 diff。
- 解决冲突并运行测试,确认结果后提交一个新的 commit。
如果不需要保留原 branch 的历史,优先使用”完整 diff + 单次提交”。需要逐个保留提交时,可以先导入旧对象,再使用 cherry-pick 或 format-patch。
为什么不能只修改 origin
origin 只是 remote 的名称和地址。修改它不会让两个 repository 建立祖先关系。
git fetch 不要求双方具有共同历史,因此可以正常取得另一个 repository 的对象。容易失败的是 merge、pull 和 rebase。面对没有共同祖先的历史,Git 无法自动判断哪些内容属于同一条开发线。
git merge --allow-unrelated-histories 虽然可以强行合并,但通常不适合这种情况。两个 repository 中的绝大多数文件都会被视为分别从空目录添加,可能产生大量没有实际意义的冲突,还会永久连接两套互不相关的历史。
第一步:保存旧 repository
开始前,先确认工作区没有未提交的内容:
git status --short
未跟踪文件不会进入 branch 历史,也不会出现在 git diff 中。需要迁移这些文件时,应先单独检查,再决定提交还是另行复制。它们不会自动随 branch 迁移。
可以在旧 repository 中为当前状态添加保护 tag:
git tag safety/before-repository-migration
接着打包基线 branch 和系统专用 branches。假设旧基线是 main,专用 branches 是 system-a 和 system-debug:
git bundle create airgap-branches.bundle \
refs/heads/main \
refs/heads/system-a \
refs/heads/system-debug \
refs/tags/safety/before-repository-migration
验证 bundle:
git bundle verify airgap-branches.bundle
git bundle list-heads airgap-branches.bundle
通过移动介质传输时,建议同时计算校验值:
sha256sum airgap-branches.bundle
目标系统收到文件后,应重新计算并核对校验值。macOS 可以使用:
shasum -a 256 airgap-branches.bundle
第二步:将旧 branches 导入新 repository
在新 repository 中导入 bundle。为了避免覆盖现有的本地 branches,可以将它们放在 refs/remotes/old-airgap/ 下:
git fetch /path/to/airgap-branches.bundle \
refs/heads/main:refs/remotes/old-airgap/main \
refs/heads/system-a:refs/remotes/old-airgap/system-a \
refs/heads/system-debug:refs/remotes/old-airgap/system-debug
检查导入结果:
git branch --remotes
git log --oneline --decorate old-airgap/system-a
这项操作不要求新旧 repository 存在共同 commit。导入旧对象后,Git 还可以在稍后执行 git apply --3way 时读取旧 patch 的基线 blob,提高三方合并成功的概率。
第三步:确定正确的旧基线
基线选得是否正确,会直接影响迁移结果。
假设 system-a 从旧 main 分出,可以先查看两者的 merge base:
git merge-base old-airgap/main old-airgap/system-a
如果该 commit 确实是系统专用开发开始前的状态,就可以生成从 merge base 到 branch tip 的最终差异:
git diff --binary --full-index --find-renames \
old-airgap/main...old-airgap/system-a \
> system-a.patch
在 git diff 中,三个点 A...B 表示比较 A 与 B 的 merge base 和 B,适合提取 branch 从分叉点开始积累的最终改动。
如果 system-a 长期没有同步旧 main,patch 中可能还包含较旧的公共代码。将其应用到新 repository 的最新 main 时,部分改动可能已经存在,或者已经采用其他方式实现。
如果系统专用 branch 定期合并旧 main,而且当前旧 main 正是其准确基线,可以直接比较两个 branch tip:
git diff --binary --full-index --find-renames \
old-airgap/main \
old-airgap/system-a \
> system-a.patch
无法确认基线时,先查看提交关系和改动范围:
git log --graph --oneline --decorate \
old-airgap/main old-airgap/system-a
git diff --stat old-airgap/main...old-airgap/system-a
git diff --name-status old-airgap/main...old-airgap/system-a
即使”两边约 98% 相同”,也不能直接把所有差异都当作系统专用改动。其中可能混有此前漏掉的公共 patch、生成文件或本地配置。
第四步:在新基线上应用 patch
基于新 repository 的目标 branch 创建迁移 branch:
git switch main
git switch -c system-a-migrated
确认工作区和 index 均处于干净状态:
git status --short
先检查 patch:
git apply --check --3way --index system-a.patch
检查通过后正式应用:
git apply --3way --index system-a.patch
如果 Git 自动完成了三方合并,修改会进入 index。检查实际结果:
git status
git diff --cached --stat
git diff --cached
git diff --cached --check
确认无误后提交:
git commit -m "Reapply air-gapped system-a changes"
每个专用 branch 都应基于合适的新基线单独创建和迁移,避免将多个 branch 的差异混入同一个 branch。
发生冲突时如何处理
git apply --3way 可能会留下冲突标记。先找出尚未合并的文件:
git status
git diff --name-only --diff-filter=U
逐个编辑冲突文件,再将其标记为已解决:
git add path/to/resolved-file
如果某个文件应该删除:
git rm path/to/file
全部解决后执行检查:
git diff --cached --check
git status
然后提交:
git commit -m "Reapply air-gapped system-a changes"
处理这类冲突时,不能机械地选择 ours 或 theirs。新 repository 的公共代码可能已经修复了旧 branch 修改的问题,也可能更改了相关接口。每一处冲突都要根据原改动的实际意图处理。
需要逐个保留改动时使用 cherry-pick
如果旧 branch 中的 commits 能清楚对应各项系统专用修改,可以在导入 bundle 后逐个执行 cherry-pick。新旧 repository 没有共同祖先并不影响这项操作,因为 cherry-pick 应用的是每个 commit 引入的 patch。
先查看需要迁移的 commits:
git log --reverse --oneline \
old-airgap/main..old-airgap/system-a
在新 branch 上按拓扑顺序应用非 merge commits:
git rev-list --reverse --topo-order --no-merges \
old-airgap/main..old-airgap/system-a |
git cherry-pick --stdin
出现冲突时:
git status
git add path/to/resolved-file
git cherry-pick --continue
需要放弃本轮操作时:
git cherry-pick --abort
这种方法会生成新的 commit IDs。由于 commit 的父节点和内容发生了变化,哈希值也会随之改变。
如果旧 branch 中有重要的 merge commit,简单使用 --no-merges 可能漏掉 merge 时手工解决冲突所产生的内容。此时需要逐个分析 merge commit,也可以改用前面的完整 tree diff 方法。
也可以使用 format-patch 和 git am
如果需要通过普通文件传输 commits,又不准备传输整个 bundle,可以在旧 repository 中生成邮件格式的 patch:
git format-patch --binary --full-index \
--output-directory exported-patches \
main..system-a
在新 branch 中应用:
git am --3way exported-patches/*.patch
解决冲突后继续:
git add path/to/resolved-file
git am --continue
放弃操作:
git am --abort
format-patch 默认不适合直接表达复杂的 merge 历史。如果无需保留历史,完整 tree diff 往往更简单,也能保留 branch 最终状态中由 merge 冲突解决产生的改动。
迁移后的验证
至少执行以下检查:
git status
git diff --check main...system-a-migrated
git diff --stat main...system-a-migrated
git diff --name-status main...system-a-migrated
随后运行项目已有的构建和测试命令,并人工检查:
- 二进制文件、符号链接和可执行权限是否正确。
- 文件删除和重命名是否符合预期。
- 是否遗漏了未跟踪文件。
.gitignore中的本地配置是否需要单独迁移。- Git LFS 对象是否已经传入隔离环境。
- submodule 对应的 repository 和 commit 是否能在隔离环境中访问。
git bundle 只传输 Git 对象,不会自动包含 Git LFS 的实际对象,也不会把 submodule repository 的内容嵌入主 repository。遇到这两种情况,需要分别准备离线传输方案。
后续维护建议
完成迁移后,尽量让联网端和隔离端保留同一套 Git 历史。以后可以定期使用 git bundle 进行增量传输,避免只搬运裸 diff。这样 Git 能识别已经导入的 commits,执行 merge、rebase、cherry-pick 和三方冲突处理时也更方便。
首次传输可以创建完整 bundle:
git bundle create full.bundle --all
创建后续增量 bundle 时,必须以隔离端已经确认拥有的 commit 为基础。具体排除范围要根据双方 refs 确定,不能只按日期推测。无论采用哪种流程,都应保留 bundle 校验结果、导入记录和迁移前的安全 tag,以免再次依赖人工记忆判断 patch 是否已经应用。
备注:内容仅供参考。
