You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将两个独立Git仓库合并为含完整历史的项目仓库?

将两个独立Git仓库合并为一个全局仓库并保留提交历史的方法

操作步骤:

  1. 初始化全局项目仓库

    • 进入项目根目录:cd project
    • 创建并初始化全局Git仓库:git init
  2. 导入前端仓库的完整提交历史

    • 将本地frontend仓库设为临时远程源:git remote add frontend ./frontend
    • 拉取frontend仓库的所有分支与提交记录:git fetch frontend
    • 合并frontend主分支(若分支名为master则替换):git merge --allow-unrelated-histories frontend/main --no-commit
    • 重写提交历史,将所有frontend的文件路径统一调整到frontend目录下:git filter-repo --to-subdirectory-filter frontend
    • 移除临时远程源:git remote remove frontend
  3. 导入后端仓库的完整提交历史

    • 重复类似操作处理backend仓库:
      • 添加临时远程源:git remote add backend ./backend
      • 拉取所有提交记录:git fetch backend
      • 合并主分支:git merge --allow-unrelated-histories backend/main --no-commit
      • 调整文件路径到backend目录:git filter-repo --to-subdirectory-filter backend
      • 移除临时远程源:git remote remove backend
  4. 清理原仓库的独立Git记录(可选)

    • 若无需保留frontend和backend的独立仓库历史,直接删除各自的.git文件夹:
      • rm -rf frontend/.git
      • rm -rf backend/.git
  5. 推送到远程全局仓库

    • 添加远程仓库地址:git remote add origin <你的远程仓库URL>
    • 推送所有提交历史到远程:git push -u origin main

注意事项:

  • 若原仓库的主分支不是main,请替换为实际分支名(如master)
  • git filter-repo是官方推荐的历史重写工具,需提前安装(可通过pip install git-filter-repo安装);若无法使用,也可替代为git filter-branch命令,但前者更稳定高效
  • 操作前建议备份原frontend和backend文件夹,避免数据丢失

内容的提问来源于stack exchange,提问作者Arctomachine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 11:25:19