81

If you git clone with --recursive, you can get all the git submodules too.

If I've forgotten to add this magical flag when cloning, as can happen, how do I now go and get any submodules?

Additionally, how can I set the recursive flag as a default for future clones?

alex
  • 479,566
  • 201
  • 878
  • 984
kenneth
  • 1,093
  • 1
  • 8
  • 10

4 Answers4

115

You can do it with this after a simple top-level clone:

git submodule update --init --recursive

I would not recommend making clone do this by default. The proper way to do this if you are using submodules aggressively for development and not just linking to 3rd party OSS libs on github that you may upgrade once in a blue moon, is to use git slave or subtree.

starball
  • 20,030
  • 7
  • 43
  • 238
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
15
  1. From the root of your repo:

    $ git submodule update --init --recursive
    

    That will update any and all registered submodules, initializing them if need be to the value as found in the .gitmodules file, and also recurse into complex submodules (ones with submodules of their own) and initialize and update them as well.

  2. The easiest way I know of to make cloning recursively the default would be to shadow git clone with an alias

    $ git config --global alias.clone = 'clone --recursive'
    

    As far as adding options always, I think that's the idiomatic method.

Tim Visher
  • 12,786
  • 16
  • 58
  • 66
  • 1
    Rather than shadow the 'clone' command I suggest adding a variant command `$ git config --global alias.cloner = 'clone --recursive'` – Abizern Nov 25 '10 at 16:32
  • 8
    Both of you need to remove the `=` mark: `git config --global alias.clone 'clone --recursive'` – ELLIOTTCABLE Aug 18 '11 at 17:33
  • 6
    NOTE: this answer is ***completely wrong***. git will not let you "shadow alias" an existing git command. The `alias.clone` will be completely ignored. – Jez Aug 02 '17 at 17:51
  • Supporting Jez's comment: [Is it possible to override git command by git alias?](https://stackoverflow.com/q/3538774) – Andrew T. Dec 07 '17 at 15:10
1

It appears you can't override "clone" with alias "clone", so it's a new alias (Abizern's solution) or "--recursive".

Is it possible to override git command by git alias?

Community
  • 1
  • 1
1

IIRC, git submodule init, git submodule update

Unfortunately, I do not see an option to enable recursive by default, however.

user502515
  • 4,346
  • 24
  • 20
  • You could always create a shell or git alias that performs a recursive clone. However, it may seem harder to do it in two steps here, but this way you can change the urls of the submodule repositories in the config file after the `init` but before the `update`. You need to do this for some repositories where the submodules have been set up with the url for the private repository and you need to change these to the public urls. – Abizern Nov 25 '10 at 16:23