ChatGPT解决这个技术问题 Extra ChatGPT

How to "git clone" including submodules?

How do I clone a git repository so that it also clones its submodules?

Running git clone $REPO_URL merely creates empty submodule directories.

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.
There's a chance you want to leave off --remote-submodules and just do --recurse-submodules. Beware.
@BenjaminBerman Could you elaborate as to why you wouldn't want to use git clone --recurse-submodules --remote-submodules?
@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.

M
Mathias Bynens

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

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 Sadly, no. (Not that I know of, at least.)
And logically thinking git clone --recursive will also populate any submodules of a submodule, right?
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?
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.
L
LiraNuna

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

git submodule init 
git submodule update

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.
Please read the git-submodule(1) man page (kernel.org/pub/software/scm/git/docs/git-submodule.html). You'll find out that git submodule update supports a nice parameter called --recursive.
Why not just do both of them in one command? git submodule update --init (Also see my answer).
I think its better to answer the question with these two commands. Its explains better how to accomplish the task.
@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.
V
VonC

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.


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?
@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: stackoverflow.com/a/56981834/6309)
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...
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: github.com/VioletGiraffe/TEST
@VioletGiraffe That is because you have added and committed the .gitmodules but not the gitlink (stackoverflow.com/a/16581096/6309, special entries in the index: stackoverflow.com/a/19354410/6309) Here is a repository which does have the proper gitlink registered: github.com/tiagomazzutti/antlr4dart
C
Community

[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

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.
B
Benyamin Jafari

[Quick Answer]

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

git submodule update --init --recursive

M
Mars Redwyne

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>

This was more like what I was looking for... but the submodules list their branch as 'detached'. :(
A
Antier Solutions

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

n
nweiler

Try this:

git clone --recurse-submodules

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


Note that --recurse-submodules and --recursive are equivalent aliases.
@SuperUberDuper in that case you can do git submodule update --init --recursive as explained in this answer
t
tro

I think you can go with 3 steps:

git clone
git submodule init
git submodule update

k
kaiser

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


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 Seems so - it sucks BIG TIME... and I know it.
That sounds like a bug. Did you report it to Bitbucket?
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".
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
V
Vincent Beltman

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"

4
4b0

Try this for including submodules in git repository.

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

or

git clone --recurse-submodules

R
Rajitha Udayanga

Just do these in your project directory.

$ git submodule init
$ git submodule update

This is the right answer if you had already cloned your repo before.
M
Mafii

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


F
FatalError

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

Generate SSH Key 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.


Yes, that is Permission denied (publickey). fatal: Could not read from remote repository. error
n
nzrytmn

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

4
4b0

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.


G
Gangbin chen

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.
S
Suraj Rao
git submodule foreach git pull origin master