命令
usage: git submodule [--quiet] [--cached]
or: git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
or: git submodule [--quiet] status [--cached] [--recursive] [--] [<path>...]
or: git submodule [--quiet] init [--] [<path>...]
or: git submodule [--quiet] deinit [-f|--force] (--all| [--] <path>...)
or: git submodule [--quiet] update [--init [--filter=<filter-spec>]] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]
or: git submodule [--quiet] set-branch (--default|--branch <branch>) [--] <path>
or: git submodule [--quiet] set-url [--] <path> <newurl>
or: git submodule [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
or: git submodule [--quiet] foreach [--recursive] <command>
or: git submodule [--quiet] sync [--recursive] [--] [<path>...]
or: git submodule [--quiet] absorbgitdirs [--] [<path>...]
相关文件
-
.gitmodules
[submodule "<moduleName>"] path = <moduleDir> url = <repoAddr>
-
.git/config 中有相近的 section:
[submodule "<moduleName>"] active = true url = <repoAddr>
-
.git
目录在.git/modules/<moduleName>
克隆
参考:https://stackoverflow.com/questions/3796927/how-to-git-clone-including-submodules
git clone --recurse-submodules -j8 github.com:shouce/shouce.git # 2.13+
git clone --recursive -j8 github.com:shouce/shouce.git # 1.6.5+
-j
表示子模块并发操作,每次 n 个子模块。
针对更老的版本或者以存在的库:
git clone github.com:shouce/shouce.git
cd shouce
git submodule update --init --recursive
发现一个 clone 参数 --[no-]shallow-submodules
,可以使每个子模块仓库的克隆 deepth 为 1,应该是用得上的。
已存在的项目
可能是克隆的时候没有克隆子仓库,也可能是后面添加进来的子仓库。
$ git submodule init
Submodule '<moduleName>' (<repoAddr>) registered for path '<moduleDir>'
$ git submodule update
Cloning into '<moduleFullPath>'...
Submodule path '<moduleDir>': checked out '<commitID>'
或者二合一:
git submodule update --init --recursive
后面有更新就进入子模块 git pull。
添加子模块
git submodule add -b dev --name devtools gitee.com:catroll/devtools tools/dev
# [submodule "devtools"]
# path = tools/dev
# url = gitee.com:catroll/devtools
# branch = dev
删除子模块
$ git submodule deinit <moduleDir>
Cleared directory '<moduleDir>'
Submodule '<moduleName>' (<remoteAddr>) unregistered for path '<moduleDir>'
作用:
- 清空子模块目录下的所有文件
- 去掉 .git/config 子模块配置
这个操作之后:git status
没有任何变化,.gitmodule
还保留着。
然后:
- 修改 .gitmodules
- 删除子模块目录
- 提交变更
- 推送到远程仓库