Git 子模块和子树管理项目

子模块和子树管理多存储库共有子项目。

参与开源项目开发,用Git 管理源码。多依赖和/或子项目的项目。对开源组织,社区和产品单一来源文档和依赖棘手。文档和项目碎片化和冗余,难维护。

  • 必要性

单项目为存储库内子项目,传统方法把项目复制到父存储库,如要多父项目用同子项目?复制到所有父项目,更新时,每父项目修改,不可行。父项目冗余和数据不一致,更新和维护子项目困难。

  • Git 子模块和子树

用命令把子项目添加到任意数目项目,同步更新修改:Git 子模块(submodule)和 Git 子树(subtree)。模块化支持共用代码开发工作流,Git 存储库 源码管理(source-code management)(SCM)与子树架桥梁:

Git 子模块

Git 默认包提供子模块,把 Git 存储库嵌入其他存储库。Git 子模块指向子树某次提交:

文件夹@提交 Id 格式表明是子模块,点文件夹进子树。 .gitmodules 配置文件含子模块存储库详细信息:

查看子模块 :

$ git submodule
 e23e823d3f51f5ebd731a68da05ad0371c3a0231 test (heads/master)

更新子模块 :

$ git submodule update
$ git submodule update --remote

递归克隆子模块 :

$ git clone https://github.com/cain/cain-test.git test --recursive 

先克隆父项目,再更新子模块:

$ git clone https://github.com/cain/cain-assets.git test
$ git submodule
 --e23e823d3f51f5ebd731a68da05ad0371c3a0231 test
$ git submodule init
Submodule 'test' (https://github.com/cain/cain-test.git) registered for path 'test'

$ git submodule update
Cloning into 'test'...
remote: Counting objects: 151, done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 151 (delta 18), reused 0 (delta 0), pack-reused 70
Receiving objects: 100% (151/151), 1.34 MiB | 569.00 KiB/s, done.
Resolving deltas: 100% (36/36), done.
Checking connectivity... done.
Submodule path 'test': checked out 'e23e823d3f51f5ebd731a68da05ad0371c3a0231'
或
$ git submodule update --init

如有嵌套子模块:

$ git submodule update --init --recursive

下载子模块

连续下载多个模块很枯燥,clone 和 submodule update 支持 --jobs (或 -j)参数:

如,一次下载 8 个子模块:

$ git submodule update --init --recursive -j 8
$ git clone --recursive --jobs 8 <URL to Git repo>

添加子模块:

$ git submodule add https://github.com/cain/cain-test.git test

$ git status
On branch master
 
Initial commit
 
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
 
    new file:   .gitmodules
    new file:   test

删除子模块 :

$ git rm --cached test
$ rm -rf test
或
$ git rm -f childmodule

删除.gitmodules子模块信息 

[submodule "assets"]
  path = test
  url = https://github.com/cain/cain-test.git

[submodule "test"]
  url = https://github.com/cain/cain-test.git

删除.git/config子模块信息 

[submodule "test"]
  url = https://github.com/cain/cain-test.git


Git 子树( subtree), Git 1.7.11 引入,把副本为子目录嵌入。注入和管理依赖的方法。常规提交保存外部依赖信息。Git 子树提供集成点,容易复原。

如参考 GitHub 子树教程,本地看不到 .gittrees 。子树副本, .gittrees 配置文件git-subtree 命令, /contrib/subtree 文件夹 下载 git-subtree。

父存储库中有整个子树副本,因此克隆会持续很长时间。

父存储库中添加子树,先 remote add,后 subtree add 命令:

$ git remote add remote-name <URL to Git repo>
$ git subtree add --prefix=folder/ remote-name <URL to Git repo> subtree-branchname


把整个子项目提交历史合并到父存储库。

向子树推送修改及从子树拉取修改

$ git subtree push-all

$ git subtree pull-all


工具都有优缺点。适合特性:

Git 子模块存占用空间小,只指向子项目提交链接,而 Git 子树保存整个子项目及提交历史。
Git 子模块需服务器可访问,但子树去中心化。
Git 子模块基于组件开发,而 Git 子树基于系统开发。
Git 子树不是 子模块替代项。明确说明来指导用哪种。如有归属外部库,回推代码,用 Git 子模块,因推送代码更容易。如有第三方代码,且不推送,用 Git 子树,因拉取代码更容易。

联系方式

手机:13822161573 微信:txsolarterms QQ:419396409

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值