git submodule update vs git submodule sync

时间:2017-08-14 16:23:07

标签: git git-submodules

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

2 个答案:

答案 0 :(得分:29)

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

棘手的是sync。想象一下,您使用子模块克隆项目,然后上游项目更改其中一个子模块以指向不同的URL。

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

另请注意,如果您要对子模块进行更改,即使上游从未更改过它们,您也可能希望网址不匹配...但是使用<对于这种情况,em>多个远程URL可能是更好的主意。

答案 1 :(得分:17)

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

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

相关问题