'git submodule update --init --recursive' VS 'git submodule foreach --recursive git submodule update --init'
Asked Answered
A

3

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
Aluminium answered 5/6, 2014 at 10:33 Comment(0)
H
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.

Herringbone answered 13/2, 2015 at 16:13 Comment(5)
how would you update a single submodule with recursive?Clam
@Clam cd into the submodule, then execute one of the above commandsAsepsis
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.Clam
@Clam Yes, it would be more appropriate as a new question.Herringbone
"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.Sileas
R
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
Resoluble answered 26/10, 2018 at 20:13 Comment(0)
C
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.

Celom answered 23/8, 2019 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.