首页/开发/Git 与协作速查/子模块 URL 变更与 sync
进阶与大仓实践69 条

子模块 URL 变更与 sync

.gitmodules 里的 URL 修改后,各克隆的本地配置仍指向旧地址;用 submodule sync 把新 URL 同步到 .git/config 与 .git/modules 配置。

Git 与协作速查开发更新于 2026-10-12
语法git submodule sync [--recursive]
前提先更新 .gitmodules 或直接改配置
配套git submodule update --init --recursive 重新拉取

常用示例

常用示例
git submodule sync --recursive
git submodule update --init --recursive
git submodule foreach 'git remote -v | head -1'
仓库迁移或换镜像后必须 sync,否则子模块拉取仍走旧域名,常见于内网切换场景。

易错点

  • 只改 .gitmodules 不 sync,旧克隆会继续用缓存配置里的老 URL
  • 子模块内自己改过 remote 时需单独核对,sync 以父仓库配置为准

数据来源与参考

  • Git 官方文档:git-submodule(sync)
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌