How do I clone a git repository so that it also clones its submodules?
git clone $REPO_URL merely creates empty submodule directories.
With version 2.13 of Git and later,
--recurse-submodules can be used instead of
git clone --recurse-submodules -j8 git://github.com/foo/bar.git cd bar
-j8 is an optional performance optimization that became available in version 2.8, and fetches up to 8 submodules at a time in parallel — see
With version 1.9 of Git up until version 2.12 (
-j flag only available in version 2.8+):
git clone --recursive -j8 git://github.com/foo/bar.git cd bar
With version 1.6.5 of Git and later, you can use:
git clone --recursive git://github.com/foo/bar.git cd bar
For already cloned repos, or older Git versions, use:
git clone git://github.com/foo/bar.git cd bar git submodule update --init --recursive
You have to do two things before a submodule will be filled:
git submodule init git submodule update
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
Note that Git 2.29 (Q4 2020) brings a significant optimization around submodule handling.
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).
setup_revisions()that the input is not filenames by passing
assume_dashdash,so it can avoid redundant stat for each ref.
refname_ambiguityflag to avoid filesystem lookups for each object. Similar logic can be found in cat-file, pack-objects and more.
Original answer 2010
--recursiveis specified, this command will recurse into the registered submodules, and update any nested submodules within.
With version 1.6.5 of git and later, you can do this automatically by cloning the super-project with the
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.
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!
git clone --recurse-submodulesthere was previously no way to
--remoteswitch to the implicit
git submodule updatecommand 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
git submodule updateas well on the grounds they the submodule has only just been cloned, so fetching from the remote again only serves to slow things down.
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
git submodule update.