为了更精细化的进行项目代码管理,减少项目的代码冗余,我们将一些独立的功能模块或者代码,
建立独立仓库进行维护。项目的代码仓库中会以git submodule
的方式引用这些依赖的子仓库。
这种管理方式,会用到 git submodule
的相关功能,要求大家了解和学习相关概念和命令使用方法。
本文把 submodule
的使用方法进行了总结,以方便大家学习和使用。
1. 主仓库中添加子仓库
git submodule add url
如果要指定 submodule 使用特定分支请使用以下命令:
git submodule add -b xxx_branch url
或者编辑修改 .gitmodules 文件,增加:
branch = xxx_branch
2. 同步代码
主仓库按正常方式同步
git fetch
git rebase origin/master
如果 submodule
有更新,在主仓库同步后,可以用 git status
查看到:
modified: xxxxx(new commits)
接下来使用命令:
git submodule init # 第一次获取子仓库需要执行这个 init
git submodule update # 以后更新子仓库只需要执行 update
(或者使用 git submodule update --init)
如果仓库包含多个 submodule
子仓库,在进行同步的时候,可能会要求多次输入密码
可以使用下面的方法解决:
git config --global credential.helper store
(这个命令可以让 git 工具自动保存凭据,只要正确输入一次密码,以后都不需要再次输入)
git config --global credential.helper "cache --timeout=600"
(这个命令可以让 git 缓存当前凭据,600s 超时后需要重新输入密码)
建议使用第二个命令更加安全
3. 子仓库代码提交
用正常的 git 仓库命令提交代码和走 review 流程即可
更新完子仓库后,回到主仓库的目录,用 git status 查看状态:
modified: xxxxx(new commits)
然后决定是否需要提交这个 submodule
的状态更新,如果需要就走提交和走 review 流程
4. 克隆含有子仓库的仓库
git clone --recurse-submodules url
也可以在 git clone
后,使用
git submodule update --init --recursive
初始化全部子仓库并获取全部子仓库的代码更新