63

I really like this command to fetch a repo with submodules:

git clone git@github.com:my_user/my_repo.git --recursive

However, the submodules are all set to "no branch" when they arrive, and I have to manually checkout master on each and every one. Is there a way to recursively pull submodules and automatically set the branch to master?

Anton Rudeshko
  • 1,039
  • 2
  • 11
  • 20
tedsuo
  • 3,943
  • 3
  • 17
  • 8

5 Answers5

91

After cloning the repository containing your submodules, the following command will checkout the master branch on all of these in one go:

git submodule foreach --recursive git checkout master
qbein
  • 1,027
  • 7
  • 4
73

How about:

git submodule update --init --recursive

To initialize all submodules and submodules inside submodules. Not sure if this will checkout master though.

Mauvis Ledford
  • 40,827
  • 17
  • 81
  • 86
26

The question is why you checkout master. Your sub modules are pinned to a specific sha - that's also why the sub module clones are fixed to that specific commit. By not pointing to a specific sha an external repo could easily break your builds. Most definitely not what you want. Better update consciously. Builds should be reproducible and as fix as possible.

tcurdt
  • 14,518
  • 10
  • 57
  • 72
  • 1
    I agree with that, but this is really annoying. If the specific commits belongs to a branch, why can't the submodule be checkout out in this branch at this specific commit? – Savageman May 15 '12 at 15:23
  • 4
    Because a commit ID is fixed while a branch can move. They are two different things and can point to two different snapshots of the submodule. – vocaro Nov 18 '12 at 18:43
  • 1
    As an example of why: My team and I use a central repo of submodules as a sort of _super_ repo to collect all of the smaller, related module repos to build the entire project locally. It saves the hassle of having to manually download 15+ different repos, especially if you want to move your environment to a different machine, vm etc. – Streetfights Feb 20 '18 at 23:19
  • 1
    After 10 years this is still relevant. – contributorpw Sep 04 '21 at 09:09
11

As already answered

git submodule foreach --recursive git checkout master

does the job for the branch master.

But if it is a branch that is not present in all of the submodules one can use

git submodule foreach --recursive "git checkout branchname || true"

Otherwise the command will fail on the first repo not having the specified branch.

YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
  • 1
    works 10x `git submodule foreach --recursive "git checkout master || git checkout main"`. thank you insane woke people. – ili Jun 14 '23 at 17:51
7

Perhaps you should consider gitslave as an alternative to git-submodule, depending on your development workflow it may be better (or worse). Specifically to your issue, gitslave keeps all members of the superproject on the same branch (absent specific git (not gitslave) commands to do otherwise, and even then many commands warn you when you are on different branches).

Gitslave is useful when you control and develop on the subprojects at more of less the same time as the superproject, and furthermore when you typically want to tag, branch, push, pull, etc all repositories at the same time.

git-submodule is better when you do not control the subprojects or more specifically wish to fix the subproject at a specific revision even as the subproject changes.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Seth Robertson
  • 30,608
  • 7
  • 64
  • 57
  • is it possible to specify which submodules needs to be synchronized and which doesn't? i.e. to exclude some submodules from this behaviour? – Savageman May 15 '12 at 15:27
  • @Savageman: With gitslave there are several ways to perform subset selection when executing a command over a set of slaves: regular expressions, a file containing the subset, etc. With git-submodule, it looks like you *might* be able to do it by explicitly naming each submodule on the command line, but I have not tried that. – Seth Robertson May 16 '12 at 01:09