I’d like to add an answer here that is really just a conglomerate of other answers, but I think it may be more complete.
You know you have a Git submodule when you have these two things.
-
Your
.gitmoduleshas an entry like so:[submodule "SubmoduleTestRepo"] path = SubmoduleTestRepo url = https://github.com/jzaccone/SubmoduleTestRepo.git -
You have a submodule object (named SubmoduleTestRepo in this example) in your Git repository. GitHub shows these as “submodule” objects. Or do
git submodule statusfrom a command line. Git submodule objects are special kinds of Git objects, and they hold the SHA information for a specific commit.Whenever you do a
git submodule update, it will populate your submodule with content from the commit. It knows where to find the commit because of the information in the.gitmodules.Now, all the
-bdoes is add one line in your.gitmodulesfile. So following the same example, it would look like this:[submodule "SubmoduleTestRepo"] path = SubmoduleTestRepo url = https://github.com/jzaccone/SubmoduleTestRepo.git branch = masterNote: only branch name is supported in a
.gitmodulesfile, but SHA and TAG are not supported! (instead of that, the branch’s commit of each module can be tracked and updated using “git add .“, for example likegit add ./SubmoduleTestRepo, and you do not need to change the.gitmodulesfile each time)The submodule object is still pointing at a specific commit. The only thing that the
-boption buys you is the ability to add a--remoteflag to your update as per Vogella’s answer:git submodule update --remoteInstead of populating the content of the submodule to the commit pointed to by the submodule, it replaces that commit with the latest commit on the master branch, THEN it populates the submodule with that commit. This can be done in two steps by djacobs7 answer. Since you have now updated the commit the submodule object is pointing to, you have to commit the changed submodule object into your Git repository.
git submodule add -bis not some magically way to keep everything up to date with a branch. It is simply adds information about a branch in the.gitmodulesfile and gives you the option to update the submodule object to the latest commit of a specified branch before populating it.