拉代码
- 如果首次克隆仓库及其模块,使用:
git clone --recursive 仓库地址
- 对于仓库首次拉取模块,可以使用:
git submodule update --init --recursive
- 更新子模块(适用于
git 1.8.2及以上版本)
git submodule update --recursive --remote
- 更新子模块(适用于
git 1.7.3及以上版本)
git submodule update --recursive
或者
git pull --recurse-submodules
修改模块
1. 优雅的删除子模块
# 逆初始化模块,其中{MOD_NAME}为模块目录,执行后可发现模块目录被清空
git submodule deinit {MOD_NAME}
# 删除.gitmodules中记录的模块信息(--cached选项清除.git/modules中的缓存)
git rm --cached {MOD_NAME}
# 提交更改到代码库,可观察到'.gitmodules'内容发生变更
git commit -am "Remove a submodule."
2. 修改某模块URL
- 修改'.gitmodules'文件中对应模块的”url“属性;
- 使用
git submodule sync命令,将新的URL更新到文件.git/config;
thinker-g@localhost: ~/app$ git submodule sync
Synchronizing submodule url for 'gitmods/thinker_g/Helpers'
thinker-g@localhost: ~/app$ # 运行后可观察到'.git/config'中对应模块的url属性被更新
thinker-g@localhost: ~/app$ git commit -am "Update submodule url." # 提交变更
PS: 本实验使用git 2.7.4 完成,较低版本git可能不能自动更新.git/config文件,需要修修改完".gitmodule"文件后手动修改.git/config.