2419

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, http://github.com/cwolves/sequelize/tree/master/lib/, nodejs-mysql-native is pointing at an external git submodule, but when I checkout the sequelize project, that folder is empty.

Dharman
  • 26,923
  • 21
  • 73
  • 125
None
  • 1
  • 30
  • 155
  • 213
  • 8
    That command would be `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](https://stackoverflow.com/a/3797061/6309). – VonC Jul 22 '19 at 15:46
  • 1
    There's a chance you want to leave off `--remote-submodules` and just do `--recurse-submodules`. Beware. – Benjamin Berman Dec 23 '20 at 21:50
  • 4
    @BenjaminBerman Could you elaborate as to why you wouldn't want to use `git clone --recurse-submodules --remote-submodules`? – Liam Crowley Mar 05 '21 at 14:55
  • 3
    @LiamCrowley , the parent (hosting, containing) repo might depend on a particular version of the submodule for a variety of reasons. For example, the maintainers of the host repo might not be ready to deal with updates just yet. – arkadianriver May 21 '21 at 12:13

19 Answers19

3437

With version 2.13 of Git and later, --recurse-submodules can be used instead of --recursive:

git clone --recurse-submodules -j8 git://github.com/foo/bar.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://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
Mathias Bynens
  • 137,577
  • 52
  • 212
  • 242
  • 180
    Is there any way to specify this behavior as default in your git repository, so that less-informed cloners will automatically get an initialized submodule? – NHDaly Feb 20 '13 at 06:37
  • 18
    @NHDaly Sadly, no. (Not that I know of, at least.) – Mathias Bynens Feb 20 '13 at 07:25
  • 7
    And logically thinking git clone --recursive will also populate any submodules of a submodule, right? – jayarjo Apr 10 '13 at 17:31
  • 3
    @NHDaly it seems not: http://stackoverflow.com/questions/4251940/retrospectively-add-recursive-to-a-git-repo – Ciro Santilli Путлер Капут 六四事 May 04 '13 at 13:28
  • 3
    you may want to consider git subtree https://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/ It solves the problem in @NHDaly comment – piccolbo Jan 02 '14 at 22:03
  • 2
    @NHDaly sure, just add this into your `~/.gitconfig` under the `[alias]` section: `cloner = clone --recursive` – slf Aug 25 '14 at 17:42
  • 2
    @slf While that’s helpful, it doesn’t answer @NHDaly’s question. `.gitconfig` settings are global, not just for a single repository. – Mathias Bynens Aug 25 '14 at 20:10
  • @mathius-bynens ah you are correct sir. I misread the question. – slf Aug 26 '14 at 00:15
  • 1
    After doing either of these, it's wise to switch sources to the master branch (or any branch) since your repo will be sort of headless. From the top level repo -- ```git submodule foreach git checkout master``` – 4Z4T4R Sep 05 '14 at 15:14
  • 5
    @toszter: is it so wise? What if the holding repo needs a version of a submodule that is *not* `master`? – Gauthier Apr 29 '15 at 12:27
  • @Gauthier - Thank you... if it's _any branch_ other than master, as mentioned, substitute `master` with your preferred branch name _other_ than `master`. – 4Z4T4R May 01 '15 at 02:27
  • 3
    @toszter Even so, the correct commit is not necessarily at the tip of the branch. – Gauthier May 01 '15 at 08:39
  • you can use `git submodule update --depth 10 --recursive --init` to shallow the repo – yuxuan Feb 11 '16 at 03:31
  • This doesn't work with git 2.7, but worked with git 2.9 – kmario23 Aug 25 '16 at 03:14
  • Thanks! I had forgotten to actually retrieve the submodule dependency contents with `git submodule update --init --recursive` before running `sh -c 'cd cmd/ios7crypt && go install'` – mcandre Oct 23 '16 at 22:20
  • 1
    I did `git submodule update --init --recursive` but all the submodules are in dechated head state, I have to go into each submodule and checkout master. Does anybody know why? – VaTo Mar 23 '17 at 02:46
  • 2
    I have git 1.9.1 and `git clone --recursive -j8 git@bitbucket.org:repoToClone` does not work. It tells me `error: unknown switch `j' ` If I remove the `-j8` it works smoothly – desmond13 Apr 06 '17 at 14:17
  • 1
    As it seems, the flag name was updated to `--recurse-submodules` – Eugene Sep 10 '17 at 11:57
  • 12
    I think I come back to this answer once a month... Why don't git just ask upon cloning if it should also download the submodules? – rgoliveira Oct 12 '17 at 21:44
  • I suppose the -j8 option is not available for already clone repos? – Czar Pino Apr 10 '18 at 07:43
  • 1
    If the repos is not public and a token/authentication is needed, this cmd will ask you to type in again and again the access token/ or user_password for every submodule. Is there any workaround? – weefwefwqg3 Sep 10 '18 at 18:17
  • I couldn't find any deprecation note neither in the release notes nor in the source code. --recurse-submodules is the documented and preferred way, but --recursive is not deprecated. – orgads Oct 18 '18 at 07:51
  • @orgads You are right. Someone had edited my answer adding that statement and somehow it got accepted. I've fixed it now. Thanks! – Mathias Bynens Oct 19 '18 at 06:46
  • 16
    Also to make clear (since I wanted to know and couldn't find an answer except by looking at the source), the git clone `--recursive` and `--recurse-submodules` options behave identically. They result in a call to the same function. – Michael Burr Nov 28 '18 at 00:52
  • "Is there any way to specify this behavior as default in your git repository"; yes, put it in your README – user5359531 Sep 10 '19 at 23:47
  • 3
    Why isn't recursive cloning the default option? – Minh Nghĩa Jul 22 '20 at 09:16
599

You have to do two things before a submodule will be filled:

git submodule init 
git submodule update
LiraNuna
  • 61,525
  • 14
  • 115
  • 138
  • 11
    I was afraid of that... it doesn't make any sense since you're checking out a partial project in that case. I understand that the submodule updates aren't automatic, but why isn't the bound version automatically checked out?? Is there any way to force it? I have a project with 3-levels of submodules and it seems absurd to have to traverse that far just to do a checkout. – None Sep 26 '10 at 07:26
  • 13
    Please read the `git-submodule(1)` man page (http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html). You'll find out that `git submodule update` supports a nice parameter called `--recursive`. – joschi Sep 26 '10 at 07:30
  • 122
    Why not just do both of them in one command? `git submodule update --init` (Also see [my answer](http://stackoverflow.com/questions/3796927/git-clone-submodule/4438292#4438292)). – Mathias Bynens Dec 15 '10 at 13:04
  • 11
    I think its better to answer the question with these two commands. Its explains better how to accomplish the task. – schmijos Apr 17 '13 at 18:20
  • 6
    @MathiasBynens A machine that I just logged into only has git 1.5.5.6, which apparently does not support the shortened instruction, but does support it as two commands. – Jack Poulson Jul 24 '13 at 18:40
  • @MathiasBynens I think you misunderstood. ``git submodule update --init`` must be expanded to ``git submodule init; git submodule update`` in 1.5.5.6. – Jack Poulson Jul 25 '13 at 16:16
  • After I do this, then if I cd into the submodule directory and to `git pull` it says its head is detached and I can't do `git pull` in it to update, any way to fix this? – Noitidart Aug 18 '16 at 22:04
318

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.

VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • 10
    So it took Git 14 years to start adding proper support for submodules, huh. Thanks for the update! What if I already have a clone of the main repo without submodules and without a recorded SHA1, and I want to pull in the latest version of each submodule. Is it doable? – Violet Giraffe Sep 09 '19 at 09:48
  • 1
    @VioletGiraffe If that cloned repository has submodules, it has "recorded SHA1". And a `git submodule update --init --recursive --remote` should update them to the latest commit of their respective branch. (ex: https://stackoverflow.com/a/56981834/6309) – VonC Sep 09 '19 at 10:14
  • 1
    Let me clarify with an example: I have a template project on Github that uses submodules, and I even commited specific revisions of the submodules into this template repo. But when I create a new project out of this repo, none of the commands you listed (neither `clone --recurse-submodules --remote-submodules` nor `submodule update --init --recursive --remote`) let me actually fetch the subrepos. All I get is a .gitmodules file, and I couldn't find any way to init the subrepos other than manually cloning them one by one. I'd like to at least have a script to do it with `submodule foreach`... – Violet Giraffe Sep 10 '19 at 05:12
  • If you know a solution, I'd ask a separate question that you could answer. Here's the test repo that I can't find any way to init other than by hand: https://github.com/VioletGiraffe/TEST – Violet Giraffe Sep 10 '19 at 05:13
  • 1
    @VioletGiraffe That is because you have added and committed the .gitmodules but not the gitlink (https://stackoverflow.com/a/16581096/6309, special entries in the index: https://stackoverflow.com/a/19354410/6309) Here is a repository which does have the proper gitlink registered: https://github.com/tiagomazzutti/antlr4dart – VonC Sep 10 '19 at 06:20
  • @VioletGiraffe Solution: clone your repo, delete the .gitmodules, add, and commit. Then add the submodules again: `git submodule add cpputils https://github.com/VioletGiraffe/cpputils.git` and `git submodule add cpp-template-utils https://github.com/VioletGiraffe/cpp-template-utils`. Add, commit and push. You will then see your submodules, and can clone back that repository *with* said submodules. – VonC Sep 10 '19 at 06:22
  • Right, I understand that, and it works for any "normal" repo you create from scratch. I think we're looking at a Github bug with regards to handling subrepos in project templates specifically, because the original template repo has gitlinks as well, but not the new project created from this template. P. S. Thanks for the answers! – Violet Giraffe Sep 10 '19 at 07:01
  • @VioletGiraffe You could still update that project template with the process I mentioned. But yes, that looks like a bug on GitHub side. That would be interesting to illustrate/detail that in a separate question. I will answer it when I go back from work. – VonC Sep 10 '19 at 08:35
  • What exactly does `--remote-submodules`? Does it pull for each submodule the latest commit, and then update the reference in the superproject? – Aviv Cohn Jul 20 '20 at 07:02
  • @AvivCohn It does pull for each submodule the latest commit of their associated branch (`master/main` by default). To my understanding, you still need to add and commit in the parent repo, in order to record the new submodules tree SHA. – VonC Jul 20 '20 at 10:38
158

[Quick Answer]

You can use this command to clone your repo with all the submodules:

git clone --recursive YOUR-GIT-REPO-URL

Or if you have already cloned the project, you can use:

git submodule init
git submodule update
Community
  • 1
  • 1
Javier C.
  • 6,855
  • 4
  • 36
  • 51
  • On git version 2.24.3 the above command gives me the error: error: Server does not allow request for unadvertised object e635630d55682951eb2da35630d5da15b6cc Fetched in submodule path 'ui-library', but it did not contain e635630d55682951eb2da35630d5da15b6cc. Direct fetching of that commit failed. – BertC Apr 07 '21 at 10:54
86

[Quick Answer]

After cloning the parent repo (including some submodule repos), do the following:

git submodule update --init --recursive
Benyamin Jafari
  • 21,522
  • 19
  • 109
  • 128
39

Use this command to clone repo with all submodules

git clone --recurse-submodules git@gitlab.staging-host.com:yourproject

To update code for all submodules

git submodule update --recursive --remote
Antier Solutions
  • 1,094
  • 5
  • 9
39

If your submodule was added in a branch be sure to include it in your clone command...

git clone -b <branch_name> --recursive <remote> <directory>
Mars Redwyne
  • 1,249
  • 13
  • 8
  • This was more like what I was looking for... but the submodules list their branch as 'detached'. :( – AceFunk Jul 25 '19 at 13:25
34

Try this:

git clone --recurse-submodules

It automatically pulls in the submodule data assuming you have already added the submodules to the parent project.

nweiler
  • 1,062
  • 1
  • 11
  • 22
  • 39
    Note that `--recurse-submodules` and `--recursive` are [equivalent aliases](https://www.kernel.org/pub/software/scm/git/docs/git-clone.html). – Joel Purra Jan 16 '13 at 18:57
  • 1
    @SuperUberDuper in that case you can do `git submodule update --init --recursive` as explained in [this answer](https://stackoverflow.com/a/4438292/854076) – Enrico Mar 19 '16 at 15:43
29

I think you can go with 3 steps:

git clone
git submodule init
git submodule update
tro
  • 6,473
  • 6
  • 46
  • 65
muhammad ali e
  • 619
  • 6
  • 7
23

late answer

// git CLONE INCLUDE-SUBMODULES ADDRESS DESTINATION-DIRECTORY
git clone --recursive https://USERNAME@bitbucket.org/USERNAME/REPO.git DESTINATION_DIR

As I just spent a whole hour fiddling around with a friend: Even if you have Admin rights on BitBucket, always clone the ORIGINAL repository and use the password of the one who owns the repo. Annoying to find out that you ran into this minetrap :P

kaiser
  • 20,775
  • 16
  • 85
  • 105
  • That's exactly what I'm dealing with. So, are you saying that anyone who needs to develop on a bitbucket repository that has submodules must use the repository creator's credentials? Blech. – jsleuth May 29 '13 at 19:28
  • @jsleuth Seems so - it sucks BIG TIME... and I know it. – kaiser May 29 '13 at 20:35
  • That sounds like a bug. Did you report it to Bitbucket? – Mathias Bynens Sep 05 '14 at 19:51
  • @MathiasBynens Did you stumble upon this issue? It's one and a half years later and I actually don't know if this is still the case. – kaiser Sep 05 '14 at 21:31
  • @kaiser No, I just stumbled upon your answer and was surprised. – Mathias Bynens Sep 07 '14 at 12:23
  • This is the case only if you use HTTPS and the user is in the URL? The "right" way is to use the SSH auth with a private/public key pair, so the password would not be requested. – Lucas Freitas Jul 05 '16 at 07:55
  • @kaiser This answer should really be deleted – Treffynnon Feb 13 '17 at 02:46
  • 4
    It doesn't descriptively answer the OPs question, but details an unrelated bug in Bitbucket; which, incidentally, could just be shortened to "use SSH key authentication". – Treffynnon Feb 14 '17 at 03:44
  • @Treffynnon documented here as it was unclear that it is Atlassians fault. See upvotes: Seemingly other ppl have run into this, searched for this problem and were glad that SO is well indexed in search engines. – kaiser Feb 14 '17 at 07:17
  • 1
    Still not resolved on Bitbucked. I'm using relative paths to submodules and as a trick I have to perform following steps: `- git remote set-url origin git@bitbucket.org:namespace/main-repo.git` `- git submodule update --init --recursive` – niziak Feb 10 '22 at 13:22
23

You can use the --recursive flag when cloning a repository. This parameter forces git to clone all defined submodules in the repository.

git clone --recursive git@repo.org:your_repo.git

After cloning, sometimes submodules branches may be changed, so run this command after it:

git submodule foreach "git checkout master"
Vincent Beltman
  • 2,044
  • 12
  • 26
Ahmad Azimi
  • 623
  • 1
  • 7
  • 12
22

Try this for including submodules in git repository.

git clone -b <branch_name> --recursive <remote> <directory>

or

git clone --recurse-submodules
4b0
  • 20,627
  • 30
  • 92
  • 137
radhey shyam
  • 722
  • 6
  • 8
17

Just do these in your project directory.

$ git submodule init
$ git submodule update
Rajitha Udayanga
  • 1,129
  • 5
  • 21
13

Submodules parallel fetch aims at reducing the time required to fetch a repositories and all of its related submodules by enabling the fetching of multiple repositories at once. This can be accomplished by using the new --jobs option, e.g.:

git fetch --recurse-submodules --jobs=4

According to Git team, this can substantially speed up updating repositories that contain many submodules. When using --recurse-submodules without the new --jobs option, Git will fetch submodules one by one.

Source: http://www.infoq.com/news/2016/03/git28-released

Mafii
  • 6,734
  • 1
  • 37
  • 54
Long Nguyen
  • 147
  • 1
  • 3
11

I had the same problem for a GitHub repository. My account was missing SSH Key. The process is

  1. Generate SSH Key
  2. Adding a new SSH key to your GitHub account

Then, you can clone the repository with submodules (git clone --recursive YOUR-GIT-REPO-URL)

or

Run git submodule init and git submodule update to fetch submodules in already cloned repository.

FatalError
  • 875
  • 11
  • 26
  • Yes, that is `Permission denied (publickey). fatal: Could not read from remote repository.` error – Ender Nov 14 '19 at 09:34
10

If it is a new project simply you can do like this :

$ git clone --recurse-submodules https://github.com/chaconinc/YourProjectName 

If it is already installed than :

$ cd YourProjectName (for the cases you are not at right directory) 
$ git submodule init
$ git submodule update
nzrytmn
  • 4,937
  • 1
  • 37
  • 33
8

Try this.

git clone -b <branch_name> --recursive <remote> <directory>

If you have added the submodule in a branch make sure that you add it to the clone command.

4b0
  • 20,627
  • 30
  • 92
  • 137
Himeshika96
  • 81
  • 1
  • 1
0

1.git submodule init 2.git submodule update

or maybe git stash -u git pull origin master git stash p

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '22 at 16:05
-4
git submodule foreach git pull origin master
Suraj Rao
  • 28,850
  • 10
  • 94
  • 99
Tappas
  • 77
  • 1
  • 5