Git 2.23 (Q3 2019): if you want to clone and update the submodules to their latest revision:
git clone --recurse-submodules --remote-submodules
If you just want to clone them at their recorded SHA1:
git clone --recurse-submodules
See below.
Note that Git 2.29 (Q4 2020) brings a significant optimization around submodule handling.
See commit a462bee (06 Sep 2020) by Orgad Shaneh (orgads
).
(Merged by Junio C Hamano -- gitster
-- in commit 2ce9d4e, 18 Sep 2020)
submodule
: suppress checking for file name and ref ambiguity for object ids
Signed-off-by: Orgad Shaneh
The argv argument of collect_changed_submodules()
contains only object ids (the objects references of all the refs).
Notify setup_revisions()
that the input is not filenames by passing assume_dashdash,
so it can avoid redundant stat for each ref.
Also suppress refname_ambiguity
flag to avoid filesystem lookups for each object. Similar logic can be found in cat-file, pack-objects and more.
This change reduces the time for git fetch
(man) in my repo from 25s to 6s.
Original answer 2010
As joschi mentions in the comments, git submodule
now supports the --recursive
option (Git1.6.5 and more).
If --recursive
is specified, this command will recurse into the registered submodules, and update any nested submodules within.
See Working with git submodules recursively for the init part.
See git submodule
explained for more.
With version 1.6.5 of git and later, you can do this automatically by cloning the super-project with the –-recursive
option:
git clone --recursive git://github.com/mysociety/whatdotheyknow.git
Update 2016, with git 2.8: see "How to speed up / parallelize downloads of git submodules using git clone --recursive
?"
You can initiate fetching the submodule using multiple threads, in parallel.
For instances:
git fetch --recurse-submodules -j2
Even better, with Git 2.23 (Q3 2019), you can clone and checkout the submodule to their tracking branch in one command!
See commit 4c69101 (19 May 2019) by Ben Avison (bavison
).
(Merged by Junio C Hamano -- gitster
-- in commit 9476094, 17 Jun 2019)
clone
: add --remote-submodules
flag
When using git clone --recurse-submodules
there was previously no way to
pass a --remote
switch to the implicit git submodule update
command for
any use case where you want the submodules to be checked out on their
remote-tracking branch rather than with the SHA-1 recorded in the superproject.
This patch rectifies this situation.
It actually passes --no-fetch
to git submodule update
as well on the grounds they the submodule has only just been cloned, so fetching from the remote again only serves to slow things down.
That means:
--[no-]remote-submodules:
All submodules which are cloned will use the status of the submodule’s remote-tracking branch to update the submodule, rather than the superproject’s recorded SHA-1. Equivalent to passing --remote
to git submodule update
.
git clone --recurse-submodules --remote-submodules
(Q3 2019 Git 2.23): it will clone and update the submodules in one command. See my edited answer below. – VonC--remote-submodules
and just do--recurse-submodules
. Beware. – Benjamin Bermangit clone --recurse-submodules --remote-submodules
? – Liam Crowley