"git checkout develop --recurse-submodules" 使子模块的 HEAD 分离

"git checkout develop --recurse-submodules" make submodule's HEAD detached

我有父项目Agit branch -a:

* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/master

Adevelop 分支依赖于子模块 subA,但 master 分支不依赖。

subA有这些分支:

git branch -a:

* develop
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop
  remotes/origin/master

.gitmodules 文件:

[submodule "subA"]
        path = subA
        url = https://someDomain/suA.git
        branch = develop

问题来了,

我进入父项目的目录,从分支 develop 切换到 master,一切顺利,因为 master 分支不依赖于 subA,但是当我使用 git checkout develop --recurse-submodules切换回develop分支时,suA的分支状态总是这样,

* (HEAD detached at refs/heads/develop)
  develop
  master

我知道我可以通过git checkout develop修复它,但我想弄清楚为什么--recurse-submodules不起作用,以及是否有让子模块从父模块切换分支的解决方案项目正确。

git 版本 2.30.1 (Apple Git-130)

似乎 recurse-submodules 仅确保您将进行正确的提交,但不会在您的本地分支之间注意到这一点。

--[no-]recurse-submodules

Using --recurse-submodules will update the content of all initialized submodules according to the commit recorded in the superproject. If local modifications in a submodule would be overwritten the checkout will fail unless -f is used. If nothing (or --no-recurse-submodules) is used, the work trees of submodules will not be updated.

它只是将 HEAD 设置为子模块的已提交(在您的项目中)版本。