1

So I can clone a branch and get all the submodules with:

git clone <url> -b myBranch --recursive

This is great :) But then I want to switch to a different branch, so I do:

git checkout myBranch2

This updated the top-level of my project, but not the submodules (I did not see a recursive option here). Infact I got an output that suggests it knows that the submodules need updating, but I just don't know how to do that part:

c:\dev> git checkout myBranch2
M        SubModule1
M        SubModule2
Branch myBranch2 set up to track remote branch myBranch2 from origin.
Switched to a new branch 'myBranch2'

So then I checked the version of the SubModule1 and it is still pointing to the version that was originaly checked out (the latest) and not the version it should be pointing to (about 3 checkins older) that was associated with the branch myBranch2.

Of course I could just clone a new one and have both - this is what I would normally do, but it bugs me that I don't know how to do this... and it might be useful.

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • See if this helps, http://stackoverflow.com/questions/1030169/easy-way-pull-latest-of-all-submodules – zillani Feb 08 '17 at 12:53
  • 1
    After `git checkout` do `git submodule update --checkout --recursive` – Leon Feb 08 '17 at 12:57
  • @zillani This question almost answers my question. I am not trying to pull any changes down as such, but I might have inferred how to do this from that question... thanks – code_fodder Feb 08 '17 at 13:28
  • @Leon perfect, this is exactly what I wanted. Tried it - worked. Feel free to post that as an answer. – code_fodder Feb 08 '17 at 13:29

1 Answers1

1

After git checkout run

git submodule update --checkout --recursive

From git submodule documentation:

git submodule update ...

Update the registered submodules to match what the superproject expects by cloning missing submodules and updating the working tree of the submodules. The "updating" can be done in several ways depending on command line options and the value of submodule.<name>.update configuration variable. Supported update procedures are:

  • checkout - the commit recorded in the superproject will be checked out in the submodule on a detached HEAD. This is done when --checkout option is given, or no option is given, and submodule.<name>.update is unset, or if it is set to checkout.

  • ...

--recursive

This option is only valid for foreach, update, status and sync commands. Traverse submodules recursively. The operation is performed not only in the submodules of the current repo, but also in any nested submodules inside those submodules (and so on).

You can also set up a post-checkout hook so that updating of submodules is performed automatically.

Leon
  • 31,443
  • 4
  • 72
  • 97