66

I have git repo which has nested submodules. What is the difference between below 2 commands?

git submodule update --init --recursive

git submodule foreach --recursive git submodule update --init
exhuma
  • 20,071
  • 12
  • 90
  • 123
Manish
  • 713
  • 1
  • 5
  • 10

3 Answers3

64
git submodule update --init --recursive

The submodule update command will recurse into the registered submodules, update and init (if required) them and any nested submodules within.

git submodule foreach --recursive git submodule update --init

foreach will evaluate the command in each checked out submodule. So it will update and init (if required) each submodule and any nested submodules within due to --recursive.

So in the end, both commands will achieve the same thing. Simply the execution differs, the first command won't step into each directory to execute the command.

Martin
  • 3,960
  • 7
  • 43
  • 43
  • 3
    how would you update a single submodule with recursive? – malhal Aug 13 '16 at 21:39
  • 2
    @malhal cd into the submodule, then execute one of the above commands – jsears May 03 '17 at 15:16
  • Is it possible to remote update a submodule and regular update its submodules, without cd'ing into sub's dir? If you remote update recursively then you get sub-sub versions later than the sub requires. Also if you use the command in the answer, then you don't get the newer version of the sub Maybe I should post a new question. – malhal Oct 16 '17 at 18:15
  • @malhal Yes, it would be more appropriate as a new question. – Martin Oct 20 '17 at 14:38
  • 2
    "So in the end, both commands will achieve the same thing." NO THEY DO NOT! E.g. take a repo with a submodule, remove submodule folder or some of its content, run the second command: Nothing happens. Run the first command: The submodule is restored. – Jack Miller Aug 28 '19 at 07:52
10

In my experience, the first one works. The second one does nothing.

For a project like eclipse.platform.releng.aggregator to initialize submodules so you can build, you need to clone all the child repos:

 git submodule update --init --recursive
nickboldt
  • 732
  • 1
  • 7
  • 13
6

There exist differences!

 git submodule update --init --recursive

will register direct dependent submodules and clone them down, then go into next depth, register submodules and clone them recursively. Finally, all directly or indirectly dependent submodules will be registered and cloned from the remote. If there exists cyclic dependency, this command will never terminate.

git submodule foreach --recursive git submodule update --init

This command follows the template:

git submodule foreach --recursive "your command"

which means that firstly, "git submodule foreach --recursive" will generate a submodules set, then in each submodule, your command gets executed. However for a initial project without executing "git submodule init" and then "git submodule update", "git submodule foreach --recursive" will be empty, so "your command" won't take place at all.