0

As per documentation man git submodule update, if run with an option --checkout then it should leave submodules's HEAD in a detached state.

       --checkout
           This option is only valid for the update command. Checkout the commit recorded in the superproject on a detached HEAD in the submodule. This is the default behavior, the main use of this option is to override submodule.$name.update when set to a value other than
           checkout. If the key submodule.$name.update is either not explicitly set or set to checkout, this option is implicit.
$ pwd
/home/user/my_project

$ git branch --show-current
main

$ cat .gitmodules 
[submodule "my_submodule"]
    path = my_submodule
    url = git@git.server:user/my_submodule.git

# Note that HEAD is not detached
$ ( cd my_submodule/ && git branch --show-current )
main

$ git submodule update --init --checkout

# Now I expect a HEAD to be in a detached state but it is not. Why?
$ ( cd my_submodule/ && git branch --show-current )
main

$ git --version
git version 2.41.0

Is my interpretation of the document wrong? May someone please clarify it for me?

TruLa
  • 1,031
  • 11
  • 21
  • 1
    Is there any update at all on your submodule when you run `update ...` on your example ? you may try to manually set `main` to a previous commit, then run again `submodule update` to see how git behaves when it actually updates something. – LeGEC Aug 05 '23 at 13:50
  • 2
    No, indeed there does not seem to be any update when `submodule update ...` command is invoked. I can confirm that setting `main` to a previous commit makes `git` behave as expected, it returns back the commit and leaves a detached HEAD as expected. I was just expecting `git` to behave consistently and leave detached HEAD unconditionally. At least this was my interpretation of the man page. – TruLa Aug 05 '23 at 14:07

1 Answers1

1

As mentioned in the comments, if the submodule is already at the commit specified in the superproject, git submodule update --init --checkout does not detach the submodule's HEAD.

However, git submodule update --init --checkout --force does detach it.

philb
  • 2,031
  • 15
  • 20