14

Reading the documentation of the git submodule command I found out that you can specify a branch for a submodule using

git submodule set-branch -branch <branch name> -- <submodule path>

This results in a config entry in the .gitmodules file

> git config --file .gitmodules --get-regex branch
...
submodule.<submodule name>.branch = <branch name>

However the documentation did not describe what this data is used for. Submodules are still checked out in the detached head state an I've failed to find a way to use this info to do a checkout for the submodule from the parent repository that sets up the submodule in a state that tracks a branch without doing a checkout inside the submodule directly.

What's the benefit of providing this info (other than the .gitmodules file containing info that could be retrieved and used by my own script)? (Or did I simply do the initialisation/update of the submodules the wrong way?)

fabian
  • 193

2 Answers2

15

If you add the submodule with --branch option you can use --remote option to update your submodule to the latest commit of that branch.

git submodule add -b example URLHERE 
git submodule init
git submodule update --recursive --remote
HoD
  • 3,522
1

One would use git submodules in order to maintain strict version management over your external dependencies.

Git submodules allow you to keep, as a subdirectory of your repository, another repository at a particular snapshot in time, in this way incorporating and tracking version history of the external code.

A git submodule points to a specific commit in an external repository. It is static and only tracks specific commits, but it does not track git refs or branches and is not automatically updated when that repository is updated. This is the very reason why one would use branches.

harrymc
  • 498,455