You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git submodule update如何指定特定分支?

Git Submodule Update 无法指定自定义分支的问题

git submodule update 命令本身没有直接指定子模块分支的选项,存在以下痛点:

  • 默认行为会拉取子模块的所有分支,对大型仓库来说,会占用大量存储空间,且更新耗时久
  • 使用--single-branch参数后,只会拉取子模块的默认分支,而非你在.gitmodules中配置的自定义分支
  • 即便在.gitmodules里明确指定了分支,这个配置也会被--single-branch忽略

举个实际例子:
假设子模块的默认分支是master,你的.gitmodules配置如下:

[submodule "my/path/to/sub"]
    path = my/path/to/sub
    url = git@github.com:org/sub.git
    branch = 16.0

当你执行git submodule update --init --single-branch时,预期拉取16.0分支,但实际会先拉取master分支,之后才检出超项目所引用的特定提交。

可行解决办法

如果需要拉取指定分支并更新,可以分两步操作:

  1. 进入子模块目录,切换到目标分支并拉取最新代码:
cd my/path/to/sub
git checkout 16.0
git pull origin 16.0
  1. 返回主仓库,提交子模块的更新:
cd ../..
git add my/path/to/sub
git commit -m "Update submodule to 16.0 branch"

或者,初始化子模块时直接指定分支:

git submodule add -b 16.0 git@github.com:org/sub.git my/path/to/sub

之后执行git submodule update --remote就能拉取该分支的最新代码。

内容的提问来源于stack exchange,提问作者Andrius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:22:03