Search code examples
gitgit-submodules

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


I have parent project A, git branch -a:

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

A's develop branch depends on a submodule subA, but master branch doesn't.

and subA have these branches:

git branch -a:

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

the .gitmodules file :

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

here's the problem,

I get into the parent project's directory, switch from branch develop to master, everything goes well cause master branch doesn't depend on subA, but when I use git checkout develop --recurse-submodules to switch back to the develop branch, the suA's branch status is always like this,

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

I know I can fix it by git checkout develop, but I want to figure it out why --recurse-submodules doesn't work, and whether is there a solution to let submodules switch branches from the parent projects correctly.

git version 2.30.1 (Apple Git-130)


Solution

  • It seems that recurse-submodules only makes sure that you will be on the right commit but won't notice this inbetween your local branches.

    --[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.

    It is simply setting the HEAD to the commited (in your project) version of your submodule.