将大仓库拆分为多个小仓库

如何使用 Git 拆库(将大仓库拆分为多个小仓库)

拆库是将一个大型 Git 仓库拆分成多个独立仓库的过程,通常用于提高团队协作效率、简化权限管理或分离不相关的项目。以下是几种常用的拆库方法:


方法一:使用 git filter-repo(推荐)

git filter-repo 是官方推荐的替代 git filter-branch 的工具,性能更好且更安全。

步骤

  1. 安装 git-filter-repo

    复制代码
    # 方法1:使用 pip 安装(推荐)
    pip install git-filter-repo
    
    # 方法2:手动下载
    # 从 https://github.com/newren/git-filter-repo 下载并安装
    复制代码
  2. 克隆原仓库(避免直接操作原仓库)

    复制代码
    git clone /path/to/original-repo new-repo
    cd new-repo
    复制代码
  3. 拆分子目录为独立仓库

    复制代码
    git filter-repo --path path/to/subdirectory/ --path-rename path/to/subdirectory/:
    • --path:指定要拆分的子目录
    • --path-rename:可选,用于重命名路径(如移除前缀)
  4. 推送新仓库

    复制代码
    git remote add origin https://github.com/your-username/new-repo.git
    git push -u origin main  # 或 master,取决于你的分支名
    复制代码

方法二:使用 git subtree split

适用于需要保留部分历史记录的场景。

步骤

  1. 在原仓库中创建临时分支

    复制代码
    git clone /path/to/original-repo
    cd original-repo
    git subtree split -P path/to/subdirectory/ -b split-branch
    • -P:指定要拆分的子目录
    • -b:创建临时分支
  2. 创建新仓库并推送

    复制代码
    mkdir new-repo
    cd new-repo
    git init
    git pull /path/to/original-repo split-branch
    git remote add origin https://github.com/your-username/new-repo.git
    git push -u origin main
    复制代码

方法三:手动拆分(适用于简单场景)

如果历史记录不重要,可以手动复制文件并初始化新仓库。

步骤

  1. 创建新文件夹

    复制代码
    mkdir new-repo
    cd new-repo
    git init
    复制代码
  2. 从原仓库复制文件

    复制代码
    cp -r /path/to/original-repo/path/to/subdirectory/* .
    复制代码
  3. 提交并推送

    复制代码
    git add .
    git commit -m "Initial commit from split"
    git remote add origin https://github.com/your-username/new-repo.git
    git push -u origin main
    复制代码

拆分后的处理

1. 更新原仓库(可选)

如果希望原仓库引用新拆分的仓库,可以使用 子模块(Submodule) 或 子树(Subtree):

复制代码
# 使用子模块
git rm -r path/to/subdirectory/
git commit -m "Remove split-out subdirectory"
git submodule add https://github.com/your-username/new-repo.git path/to/subdirectory/
git commit -m "Add new-repo as submodule"

# 使用子树(更推荐)
git remote add new-repo https://github.com/your-username/new-repo.git
git fetch new-repo
git subtree add --prefix=path/to/subdirectory/ new-repo main --squash

2. 处理历史记录

  • git filter-repo 会保留完整历史记录(仅针对拆分的文件)。
  • 如果原仓库有大量无关历史,拆分后新仓库会更干净。

注意事项

  1. 备份原仓库:拆分操作会重写历史,务必先备份!

  2. 大文件处理 :如果原仓库有 git-lfs 文件,需确保新仓库也配置 git-lfs。

  3. 分支和标签 :默认情况下,git filter-repo 会处理所有分支和标签。如果需要限制,可以添加 --ref 参数:

    复制代码
    git filter-repo --path path/to/subdirectory/ --ref refs/heads/main
    复制代码

总结

方法 适用场景 优点 缺点
git filter-repo 需要完整历史记录 性能好、安全 需要安装
git subtree split 保留部分历史 无需额外工具 操作稍复杂
手动拆分 简单场景 快速 无历史记录

推荐使用 git filter-repo,它是目前最强大、最安全的拆库工具。

相关推荐
Hvitur2 天前
Git - 多分支合并
git·github
考虑考虑2 天前
git刷新远程分支列表
git·后端·gitlab
AI大模型-小华2 天前
Codex 自动测试怎么用?从生成测试用例到修复失败用例
git·测试用例·ai编程·软件开发·codex·chatgpt pro·codex cli
七分辣度3 天前
GitHub/Git 常用命令及使用场景
git·github
慧都小项4 天前
MyEclipse Inline Diff 实用指南:逐处核对 AI 修改再提交
git·ai编程·myeclipse·java 代码审查·ai 代码修改
德育处主任4 天前
让 Claude Code 在你睡觉时接着干活:tmux、权限策略与验收清单
git·ai编程·命令行
为思念酝酿的痛6 天前
Git基本操作
git·github
wno7047 天前
常用的Git命令清单
大数据·git·elasticsearch
真上帝的左手8 天前
3. 代码管理-Git-进阶
git·源代码管理
YIN_尹8 天前
【git原理与使用】git基本操作及底层原理(二)
大数据·git·elasticsearch