Git-子模块

Git子模块可以用于管理多个依赖关联的或者拆分成多个子项目的项目

基本命令

init / update

初始化 更新 克隆项目

  • 初始化子模块 添加一个项目到当前目录 同时当前目录下会生成一个 .gitmodule 文件

    1
    git submodule add https://git.i.sixi.com/Crm/crm-customer-service.git
  • 克隆一个子模块项目

    1
    2
    3
    git clone https://git.i.sixi.com/sixi-micro-service-crm.git
    cd sixi-micro-service-crm
    git submodule update --init
  • 或者直接 克隆的时候添加参数 --recursive

    1
    git clone --recursive https://git.i.sixi.com/sixi-micro-service-crm.git

status

查看Git子模块状态

1
2
3
4
5
6
git submodule status
90748fad7d94f362481871e76f2162069db1811d sixi-micro-service-app-api-parent (heads/master)
3348e77b171208cd52500ece18b2682c8f1cf1d6 sixi-micro-service-app-parent (heads/master)
be02965778189cd876b1d4483c0aa54ed68dac93 sixi-micro-service-common (heads/master)
c5eafe04911efe174ed4f58857b7ae9d2279d851 sixi-micro-service-common-upload (heads/master)
50842597cf0c3aad57200b628286ddfb2ce1e274 sixi-micro-service-template (heads/master)

summary

查看子模块汇总统计 (Commit更新)

1
2
3
4
5
6
7
8
9
git submodule summary
* pay-core-service 0000000...f692e83 (1):
> Create & Init pay-core-service Project...

* pay-duolaibei-service 0000000...2b1930e (1):
> Create & Init pay-duolaibei-service Project...

* pay-duolaibei-service-api 0000000...edce540 (1):
> Create & Init pay-duolaibei-service-api Project...

sync

同步子模块信息

1
2
3
4
git submodule sync
Synchronizing submodule url for 'pay-core-service'
Synchronizing submodule url for 'pay-duolaibei-service'
Synchronizing submodule url for 'pay-duolaibei-service-api'

foreach

在各个子模块目录批量执行命令

1
git submodule foreach git checkout master # 批量切换子模块到master

高级使用

  • 批量更新子项目代码
    1
    git submodule foreach git pull

参考资料