Git Setting a submodule to follow a branch


Example

A submodule is always checked out at a specific commit SHA1 (the "gitlink", special entry in the index of the parent repo)

But one can request to update that submodule to the latest commit of a branch of the submodule remote repo.

Rather than going in each submodule, doing a git checkout abranch --track origin/abranch, git pull, you can simply do (from the parent repo) a:

git submodule update --remote --recursive

Since the SHA1 of the submodule would change, you would still need to follow that with:

git add .
git commit -m "update submodules"

That supposes the submodules were:

  • either added with a branch to follow:

      git submodule -b abranch -- /url/of/submodule/repo
    
  • or configured (for an existing submodule) to follow a branch:

      cd /path/to/parent/repo
      git config -f .gitmodules submodule.asubmodule.branch abranch