Git Submodule Update 命令备忘笔记
核心命令差异分析 git submodule update 行为:将子模块检出到父仓库记录的特定提交(commit hash) 特点:保持与父仓库 .gitmodules 和索引中记录的版本一致 适用场景:需要精确重现项目某个时间点的状态 局限性:不会拉取远程最新提交,只同步到父仓库索引中记录的版本 git submodule update --remote --recursive 行为:将子模块更新到远程分支的最新提交 特点:忽略父仓库记录的提交,直接拉取远程最新版本 适用场景:需要获取子模块的最新开发进度 常见困惑:为什么普通 update 拉取不到最新版本? 问题现象 许多开发者会遇到这种情况: # 执行普通更新,发现子模块没有更新到最新版本 git submodule update # 但是使用 --remote 参数却能拉取到最新版本 git submodule update --remote --recursive 根本原因分析 这个现象的核心在于 Git 子模块的版本绑定机制: 父仓库记录的是具体提交:当你添加子模块时,父仓库会在其索引中记录子模块的具体 commit hash 普通 update 遵循绑定版本:git submodule update 只会将子模块检出到父仓库记录的那个具体提交 –remote 忽略绑定版本:git submodule update --remote 会直接从远程仓库拉取最新提交 实际演示场景 # 查看当前子模块状态 git submodule status # 输出示例:-a1b2c3d4 path/to/submodule (v1.0-5-ga1b2c3d) # 父仓库记录的是 a1b2c3d4 这个提交 # 即使远程仓库已经有新的提交 e5f6g7h8 # 使用普通 update git submodule update # 子模块仍然停留在 a1b2c3d4 # 使用 --remote 参数 git submodule update --remote # 子模块更新到最新的 e5f6g7h8 详细参数说明 --remote 参数 # 更新到远程分支最新版本 git submodule update --remote # 指定远程分支 git submodule update --remote --branch main --recursive 参数 # 递归更新嵌套的子模块 git submodule update --recursive # 组合使用 git submodule update --remote --recursive 其他常用参数 # 强制更新(丢弃本地修改) git submodule update --force # 初始化并更新 git submodule update --init --recursive # 并行更新(提高速度) git submodule update --jobs 4 配置选项管理 局部配置(仅当前仓库) 设置子模块默认更新行为 # 设置特定子模块跟踪远程分支 git config -f ....