git submodule tracking latest
Asked Answered
B

1

183

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?

Banna answered 8/2, 2012 at 8:0 Comment(2)
While you asked for a tool, I just want to connect this question which collects one-liners doing the same thing: stackoverflow.com/questions/1030169/…Felix
Git now offer tracking the latest with submodules: see my edited answer.Stoplight
S
300

Edit (2020.12.28): GitHub change default master branch to main branch since October 2020. See https://github.com/github/renaming
This answer below still reflect the old naming convention.


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 

If you had a submodule already present you now wish would track a branch, see "how to make an existing submodule track a branch".

Also see Vogella's tutorial on submodules for general information on submodules.

Note:

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

Signed-off-by: W. Trevor King

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.


Original answer (February 2012):

A submodule is a single commit referenced by a parent repo.
Since it is a Git repo 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 of 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.

Stoplight answered 8/2, 2012 at 8:24 Comment(12)
You're absolutely right, it was a pebcak. I typed remote instead of submodule while trying to add a submodule. A silly mistake.Jughead
@BraveNewMath you need to checkout the right branch in your submodule, then go to your parent repo and type: git config -f .gitmodules submodule.<path>.branch <branch>. Add everything, commit and push.Stoplight
@BraveNewMath I detail all the steps for making a submodule track a branch in https://mcmap.net/q/13074/-how-can-i-specify-a-branch-tag-when-adding-a-git-submodule.Stoplight
It's important to use the --remote tag if you don't want to get detached heads when you update, wondering why it seems your freshly pulled code is behind master!Evangelinaevangeline
I had already committed so used this git push origin HEAD:masterOutrage
@MarceloFilho Err... De nada?Stoplight
@MarceloFilho I do not speak portuguese, unfortunately. As for the --remote, be aware of that design choice: https://mcmap.net/q/13322/-git-track-branch-in-submodule-but-commit-in-other-submodule-possibly-nested. Even without a specified branch, a submodule would still update itself when you are using the --remote.Stoplight
Sure @VonC, I'm implementing some Jenkins automatic jobs in order to compile or not with a submodule commit or the HEAD of some submodules. With this flag I can easily back to the specific one or get the HEAD. Thanks.Exciter
I have tried every possible method of getting a submodule to track a remote branch, but according to this answer, submodules are always checked out in a headless state at clone time (and when updated too from what I understand). This feature was supposedly removed from git circa 2014 but answers most answers do not seem to reflect this.Palaeogene
@DC_ I agree with "this answer" (since I wrote it). The "following a branch" feature is meant to update a submodule to the latest commit of a branch. Once that is done, you will still have to add and commit the new submodule state in the parent repo. And the next clone will checkout that state (headless).Stoplight
@Stoplight So to further clarify, the "following a branch" feature only affects the behavior of the git submodule update command by telling it which commit (i.e. the most recent commit on master) to update the submodule to, not automagically cause the parent repo to update which commit is pointed to by the submodule at clone time?Palaeogene
@DC_ Yes: whenever a submodule change, the parent still has to record that new state (gitlink) from the submodule.Stoplight

© 2022 - 2024 — McMap. All rights reserved.