How to "Git Clone" Including Submodules?
I'm Trying to Put a Submodule into a Repo. the Problem Is That When I Clone the Parent Repo, the Submodule Folder Is Entirely Empty. Is There Any Way to Make...
I'm trying to put a submodule into a repo. The problem is that when I clone the parent repo, the submodule folder is entirely empty.
Is there any way to make it so that git clone parent_repo actually puts data in the submodule folder?
For example, , nodejs-mysql-native is pointing at an external git submodule, but when I checkout the sequelize project, that folder is empty.
19 Answers
With version 2.13 of Git and later, --recurse-submodules can be used instead of --recursive:
git clone --recurse-submodules -j8 git://
cd bar
Editor’s note: -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 man git-clone.
With version 1.9 of Git up until version 2.12 (-j flag only available in version 2.8+):
git clone --recursive -j8 git://
cd bar
With version 1.6.5 of Git and later, you can use:
git clone --recursive git://
cd bar
For already cloned repos, or older Git versions, use:
git clone 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
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)
Must Read
submodule: suppress checking for file name and ref ambiguity for object idsSigned-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 passingassume_dashdash,so it can avoid redundant stat for each ref.Also suppress
refname_ambiguityflag 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
--recursiveis 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
–-recursiveoption:
git clone --recursive 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)