Primoz Rome
Primoz Rome

Reputation: 11021

git submodule foreach not working

I want to figure out why git submodule foreach command is not working for me. I have cloned a git repository which has several submodules. I wanted to init and update all submodules at once to pull down the submodules sources. But whatever I try git submodule foreach is not working for me... I tried several things:

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

Every command will execute but without any output... If I go and init/update each module separately (without using foreach) then no problem.

Any ideas?

Upvotes: 5

Views: 4215

Answers (3)

Nick Woodhams
Nick Woodhams

Reputation: 12677

You have to run:

git submodule init
git submodule update

--

You need a .gitmodules file at the root fo your tree, it looks like this.

[submodule "LiveReload/Compilers"]
        path = LiveReload/Compilers
        url = git://github.com/livereload/livereload-plugins.git
[submodule "Shared/libs/fsmonitor"]
        path = Shared/libs/fsmonitor
        url = git://github.com/andreyvit/fsmonitor.c.git
[submodule "js"]
        path = js
        url = git://github.com/livereload/livereload-js.git

Here's how the output should look based on the above:

Nicks-MacBook:LiveReload2 admin$ git submodule init
Submodule 'LiveReload/Compilers' (git://github.com/livereload/livereload-plugins.git) registered for path 'LiveReload/Compilers'
Submodule 'Shared/libs/fsmonitor' (git://github.com/andreyvit/fsmonitor.c.git) registered for path 'Shared/libs/fsmonitor'
Submodule 'js' (git://github.com/livereload/livereload-js.git) registered for path 'js'
Nicks-MacBook:LiveReload2 admin$ git submodule update
Cloning into 'LiveReload/Compilers'...
remote: Counting objects: 7571, done.
remote: Compressing objects: 100% (5446/5446), done.
remote: Total 7571 (delta 2326), reused 6733 (delta 1488)
Receiving objects: 100% (7571/7571), 7.80 MiB | 2.73 MiB/s, done.
Resolving deltas: 100% (2326/2326), done.
Submodule path 'LiveReload/Compilers': checked out 'd770710edc2362caf4ed9adf303da1edc9e6e494'
Cloning into 'Shared/libs/fsmonitor'...
remote: Counting objects: 132, done.
remote: Compressing objects: 100% (79/79), done.
remote: Total 132 (delta 73), reused 112 (delta 53)
Receiving objects: 100% (132/132), 23.29 KiB, done.
Resolving deltas: 100% (73/73), done.
Submodule path 'Shared/libs/fsmonitor': checked out '1290027aea3a8e3f7fe06e3c228a16240c0fc17f'
Cloning into 'js'...
remote: Counting objects: 745, done.
remote: Compressing objects: 100% (413/413), done.
remote: Total 745 (delta 301), reused 703 (delta 259)
Receiving objects: 100% (745/745), 864.22 KiB | 820 KiB/s, done.
Resolving deltas: 100% (301/301), done.
Submodule path 'js': checked out '6aa86b01479c3aad785e9623f39cfcde2b8615f

Upvotes: 2

Adam Dymitruk
Adam Dymitruk

Reputation: 129526

After a clone all you usually need to do is:

git submodule update --init --recursive

After this is done initially, you drop the --init option.

The init option is the part of the command that copies the url specified in the .gitmodules file down into the submodule repo configuration. You may not want to do this if you are using a particular remote of choice and the project has move to one you don't want to use. A typical case of this is when you would rather use your fork of a project on GitHub and the top level repo points to the main one.

Upvotes: 4

VonC
VonC

Reputation: 1323203

git submodule init or git submodule update are supposed to work for all submodules registered in .gitmodules, so it doesn't make sense to execute them for each submodules.

If you had submodules within submodules, git submodule update --recursive would take care of all submodules recursively.

Upvotes: 7

Related Questions