1

I have a git submodule configured to track a specific branch:

[submodule "subproject"]
path = subproject
url = ../subproject.git
branch = v2.44

In my build script, I want to be able to query the branch of the subproject, so I use cd subproject && git rev-parse --abbrev-ref HEAD. I would expect this to output v2.44, but it always outputs HEAD.

When I go into subproject/ and inspect the branch, I get:

git branch -v
* (HEAD detached at b69ac07) b69ac07 Another commit
  master                     36096e7 Some commit

What's going on here? Why would I be detached rather than on the v2.44 branch?

If I manually git checkout v2.44, the parent git repo doesn't detect any sort of modification to the submodule either, versus if I were to git checkout v2.43 and then it does notice that the submodule was modified.

rgov
  • 3,516
  • 1
  • 31
  • 51

1 Answers1

2

This is normal!

The branch notation in a submodule declaration is used only when updating the submodule, via git submodule update --rebase or git submodule update --merge, or a few other special oddball cases. At all other times, each submodule has a detached HEAD, detached at the commit selected by the gitlink index entry in the superproject that is using the submodule.

(That is, in the superproject, some commit C is checked out. Commit C says: When you use submodule S, it should be on commit CS. This thing is called a gitlink and it gets copied from commit C to the index, and then git submodule update goes into the submodule and checks out that particular commit, as a detached HEAD.)

torek
  • 448,244
  • 59
  • 642
  • 775