Git submodule to track remote branch

Submodules are always checked out in a detached HEAD mode.

That is because a submodule will checkout the SHA1 stored in the special entry in the index of the parent repo.

Plus, if you want a submodule to follow the branch you have registered in the .gitmodules, you need:

git submodule update --init --remote

The --remote will make a git fetch, plus a checkout of the new HEAD.
Alas, even that checkout will be of a commit, not of the branch (since you have no local branch by default in a submodule), so... back to a detached HEAD mode.
See more at "Git submodules: Specify a branch/tag".

You can try (not tested) a:

git submodule foreach 'git checkout -b $(git config -f /path/to/parent/repo/.gitmodules --get submodule.$path.branch)'

I take advantage of the fact git submodule foreach has access to '$path', the name of the submodule directory relative to the superproject.


There was an attempt to specify a branch for a submodule to be automatically checked out in (commit 23d25e4 for Git 2.0).... but it got reversed (commit d851ffb, April 2d 2014)!
It might come soon, but not in its current implementation.


If your plan is to contribute to a submodule, the best approach is to check it out separately as a regular git repo. Do you work in branches with different remotes. Then point your submodule at the single remote you want to use for testing.