l.thee.a l.thee.a - 1 year ago 145
Git Question

git submodule tracking latest

We are moving our (huge) project to git and we are thinking about using submodules. Our plan is to have three different heads in the superproject: release,stable,latest. The project leads will handle the release and stable branches. They will move the submodules as required.

The issue is the "latest" head. We would like the superproject "latest" head to track the master branches of all the submodules (automatically). And also it would be great if it would show the history of all commits to the submodule.

I have looked at gitslave, but it is not quite what we want. Any suggestions?

Answer Source

Update March 2013

Git 1.8.2 added the possibility to track branches.

"git submodule" started learning a new mode to integrate with the tip of the remote branch (as opposed to integrating with the commit recorded in the superproject's gitlink).

# add submodule to track master branch
git submodule add -b master [URL to Git repo];

# update your submodule
git submodule update --remote 

See also the Vogella's tutorial on submodules.

See "How to make an existing submodule track a branch" (if you had a submodule already present you wish now would track a branch)


git submodule add -b . [URL to Git repo];

See git submodule man page:

A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.

See commit b928922727d6691a3bdc28160f93f25712c565f6:

submodule add: If --branch is given, record it in .gitmodules

This allows you to easily record a submodule.<name>.branch option in .gitmodules when you add a new submodule. With this patch,

$ git submodule add -b <branch> <repository> [<path>]
$ git config -f .gitmodules submodule.<path>.branch <branch>

reduces to

$ git submodule add -b <branch> <repository> [<path>]

This means that future calls to

$ git submodule update --remote ...

will get updates from the same branch that you used to initialize the submodule, which is usually what you want.

Signed-off-by: W. Trevor King

Original answer (February 2012):

A submodule is a single commit referenced by a parent repo.
Since it is a Git rpeo on its own, the "history of all commits" is accessible through a git log within that submodule.

So for a parent to track automatically the latest commit of a given branch of a submodule, it would need to:

  • cd in the submodule
  • git fetch/pull to make sure it has the latest commits on the right branch
  • cd back in the parent repo
  • add and commit in order to record the new commit if the submodule.

gitslave (that you already looked at) seems to be the best fit, including for the commit operation.

It is a little annoying to make changes to the submodule due to the requirement to check out onto the correct submodule branch, make the change, commit, and then go into the superproject and commit the commit (or at least record the new location of the submodule).

Other alternatives are detailed here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download