git 子模块更新与 git 子模块同步

git submodule update vs git submodule sync

git 文档完全没有说明 git submodule updategit submodule sync 之间的区别。我也没有在网上找到任何帮助。有人可以帮我看看这里有什么区别吗?

   update
       Update the registered submodules to match what the superproject expects
       by cloning missing submodules and updating the working tree of the
       submodules. The "updating" can be done in several ways depending on
       command line options and the value of submodule.<name>.update
       configuration variable.

-

   sync
       Synchronizes submodules' remote URL configuration setting to the value
       specified in .gitmodules. It will only affect those submodules which
       already have a URL entry in .git/config (that is the case when they are
       initialized or freshly added). This is useful when submodule URLs
       change upstream and you need to update your local repositories
       accordingly.

作为参考,我使用的是 git 客户端版本 2.11.0

git submodule update 更新子模块的 内容 。它实际上是 运行 每个子模块中的 "git fetch" 和 "git checkout"。

git submodule sync 更新关于子模块的 元数据 以反映子模块 URL 中的更改。它将 .git/config 中的信息与 .gitmodules 中的信息重新同步。

update 基本上是在每个子模块中做 git pull(除了没有分支,因为主仓库直接指定了一个提交)。

棘手的是 sync。假设您克隆了一个带有子模块的项目,然后上游项目 更改 其中一个子模块以指向不同的 URL.

您的子模块的本地副本仍将指向旧的 URL,因为 git 从不 允许远程存储库强制更改本地配置.您需要 运行 git submodule sync 将远程仓库的配置应用到本地子模块仓库。

另请注意,如果您对子模块进行更改,您可能希望 URL不匹配即使上游从未更改过它们......但是使用 multiple remote URLs 可能是那种情况下更好的主意。