git子模块更新“ no-fetch”

dtm*_*and 5 git git-submodules

当我发布子模块更新并包含“ --no-fetch”时,如下所示:

git submodule update --remote --no-fetch
Run Code Online (Sandbox Code Playgroud)

文档指出:

为了确保当前跟踪分支状态,在计算SHA-1之前,update --remote将获取子模块的远程存储库。如果您不想获取,则应使用子模块更新--remote --no-fetch。

我对“ --no-fetch”部分有些困惑。如果我在没有更新的情况下致电更新:

git submodule update --remote
Run Code Online (Sandbox Code Playgroud)

我知道将不会执行提取-但这也意味着我不能保证“ 当前跟踪分支状态 ”吗?这到底是什么意思呢?

在哪种情况下,我不希望保证当前的跟踪分支状态

小智 1

git submodule update在幕后做几件事。从git help submodule

通过克隆缺失的子模块、获取子模块中缺失的提交以及更新子模块的工作树,更新注册的子模块以匹配超级项目的期望。

所以运行git submodule update --remote大致相当于(在子模块内):

$ git fetch origin  # update remote-tracking branches
$ git checkout origin/HEAD  # update working tree
Run Code Online (Sandbox Code Playgroud)

origin/HEAD是一个远程跟踪分支,跟随远程存储库的分支(它通常是隐藏的,但您可以使用 看到它git branch --remotes)。请注意,这git fetch会引起网络活动,但git checkout完全发生在本地。

--no-fetch跳过第一步,但仍会将子模块工作树更新到远程跟踪分支。

我不认为存在您愿意的常见情况--no-fetch,但它可能在连接有限的情况下最有用。例如,在您乘坐飞机之前,您可以git fetch --recurse-submodules。然后在飞行过程中,您可以使用git submodule update --remote --no-fetch(更新到子模块远程跟踪分支)或git submodule update --no-fetch(更新到超级项目中记录的提交)而无需访问网络。但是,这不会有“当前跟踪分支状态”,因为您的远程跟踪分支只会与您上次获取的最新分支相同。