首页·终端命令

git submodule

git

用法

git submodule update --init --recursive

把另一个仓库固定在某个提交上,嵌进你的仓库里;普通克隆后那个目录是空的,要 update --init,而父仓库记住的是提交而不是分支。

常用选项

选项含义
add <url> <path>pin another repository at a path
update --initfill in a submodule folder that a clone left empty
--recursivego into submodules of submodules
update --remotemove the pin to the tip of the tracked branch
statusthe commit each submodule sits at
deinit <path>unregister it
foreach <cmd>run a command in each one

示例

git submodule update --init --recursive

the command that fills empty submodule folders after a clone

git submodule add https://github.com/x/lib.git vendor/lib

adds a dependency as a submodule

git submodule update --remote

updates the pinned commit to the branch tip

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git submodule 是做什么的?

把另一个仓库固定在某个提交上,嵌进你的仓库里;普通克隆后那个目录是空的,要 update --init,而父仓库记住的是提交而不是分支。

Q. 怎么写?

git submodule update --init --recursive —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-submodule