How does git submodule add -b
work?
After adding a submodule with a specific branch, a new cloned repository (after git submodule update --init
The only effect of choosing a branch for a submodule is that, whenever you pass the --remote
option in the git submodule update
command line, Git will check out in detached HEAD mode (if the default --checkout
behavior is selected) the latest commit of that selected remote branch.
You must be particularly careful when using this remote branch tracking feature for Git submodules if you work with shallow clones of submodules.
The branch you choose for this purpose in submodule settings IS NOT the one that will be cloned during git submodule update --remote
.
If you pass also the --depth
parameter and you do not instruct Git about which branch you want to clone -- and actually you cannot in the git submodule update
command line!! -- , it will implicitly behave like explained in the git-clone(1)
documentation for git clone --single-branch
when the explicit --branch
parameter is missing, and therefore it will clone the primary branch only.
With no surprise, after the clone stage performed by the git submodule update
command, it will finally try to check out the latest commit for the remote branch you previously set up for the submodule, and, if this is not the primary one, it is not part of your local shallow clone, and therefore it will fail with
fatal: Needed a single revision
Unable to find current origin/NotThePrimaryBranch revision in submodule path 'mySubmodule'