【问题标题】:git submodule update vs git submodule syncgit 子模块更新与 git 子模块同步
【发布时间】:2017-08-14 16:23:06
【问题描述】:

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 git-submodules


    【解决方案1】:

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

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

    【讨论】:

    • 嗯,这很有趣。 git submodule init 是否也暗示 git submodule sync?我一直在使用带有多个子模块的远程仓库并在 ​​git pull 之后执行 git submodule init; git submodule update 并且从未不同步。
    • 我不知道,但这似乎是一个相对容易的测试。
    • @BenjaminLeinweber 离题,但可以使用&amp;&amp; 而不是; 来分隔你的命令。这样,如果第一个命令失败,以下命令将不会执行。
    • @BenjaminLeinweber,您只需在每次在主存储库上执行git pull 时执行git submodule update,除非您是第一次在新克隆的主存储库上设置 git 子模块回购。
    【解决方案2】:

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

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

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

    还要注意,如果您对子模块进行更改,您可能希望 URL 不匹配,即使上游从未更改过它们...但是使用 多个远程 URL 可能是一个更好的主意。

    【讨论】:

    • 我不确定我是否理解您的 URL 不匹配评论。你能详细说明一下吗?
    • 您可能没有权限直接推送到用作子模块的存储库。所以你推送到你的 fork,然后对主子模块 repo 进行 PR。
    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 2012-09-14
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2012-07-09
    • 2017-08-13
    • 2021-12-15
    相关资源
    最近更新 更多