git submodule update vs git submodule sync

Ben*_*ber 34 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.
Run Code Online (Sandbox Code Playgroud)

-

   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.
Run Code Online (Sandbox Code Playgroud)

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

o11*_*11c 39

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

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

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

另请注意,如果要对子模块进行更改,您可能希望 URL不匹配,即使上游从未更改过它们......但使用多个远程URL可能更适合这种情况.

  • 您可能没有*权限*直接推送到用作子模块的存储库。所以你推送到你的叉子,然后对主子模块 repo 做一个 PR。 (2认同)

lar*_*sks 24

git submodule update更新子模块的内容.它在每个子模块中有效地运行"git fetch"和"git checkout".

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

  • 嗯,这很有趣。`git submodule init` 是否也暗示了一个 `git submodule sync`?我一直在使用带有多个子模块的远程仓库并执行 `git submodule init; 在`git pull`之后git submodule update`并且从未失去同步。 (2认同)
  • @BenjaminLeinweber 偏离主题,但也许使用“&amp;&amp;”而不是“;”来分隔命令。这样,如果第一个命令失败,则后面的命令将不会执行。 (2认同)