Paul Paul - 2 months ago 22
Git Question

Does git submodule branch switch along with main project's branch?

Recently I got to know git submodules. They appear to be self-sufficient projects with their own git structure and branches.

When I switch main project's branch, does this also switch submodule's branch? How does git handle this situation?


I recently came across a similar question. (I'll let you read the content of this answer as it gives you the background to my answer).

A submodule is just a reference to a repo and a commit in this repo.
When you switch branch, the reference may change (if the branch you're switching to uses a different reference) but the filesystem of the submobule will remain untouched.
So writing git status will yield a result showing that your submodule status differs from the one in your current HEAD.

To make the submodule point to the right commit for HEAD, simply invoke:

$ git submodule update 

In addition, the branch of your submodule is not interesting to the enclosing repo as the reference is always added toward a commit and not toward a branch. The branches in your submodule will therefore only reflect your branching strategy for the submodule and do not need to match the enclosing repository's branching strategy.