What Is the Point of 'Git Submodule Init'?

Background

To populate a repository's submodules, one typically invokes:

git submodule init
git submodule update

In this usage, git submodule init seems to do only one thing: populate .git/config with information that is already in .gitmodules.

What is the point of that?

Couldn't git submodule update simply use the information from .gitmodules? This would avoid both:

  • an unnecessary command (git submodule init); and
  • an unnecessary duplication of data (.gitmodules content into .git/config).

Question

Either:

  • there are use-cases for git submodule init that I do not know (in which case, please enlighten me!); or else
  • git submodule init is cruft that could be deprecated in Git without any harm.

Which of these is true?

2 Answers

Imagine the repository has 10 submodules and you are interested in only two submodules of these. In such a case, you may want to get updates from only these two submodules from the remote repository from time to time. git init works well for this, because once you execute the command git init for these two submodules, git submodule update --remote applies only to them.


Appended two workflows demo.

Workflow1: Submodules are libraries which several projects use.

I think this is one of the common use cases.

You just cloned "my-project".

git clone 

And the surface of its structure is like below.

The contents of .gitmodules

[submodule "lib1"]
    path = lib1
    url = 
[submodule "lib2"]
    path = lib2
    url = 
[submodule "lib3"]
    path = lib3
    url = 
[submodule "lib4"]
    path = lib4
    url = 

You want to refactor the code code1.js which references lib1 and lib2 which means you don't need to clone and checkout lib3 and lib4. So you just run the below command.

git submodule init lib1 lib2

Now let's see the contents of .git/config

...
[submodule "lib1"]
    active = true
    url = 
[submodule "lib2"]
    active = true
    url = 

This means something like "Ready to update lib1 and lib2 from ".

At this point, lib1 and lib2 directories are empty. You can clone and checkout lib1 and lib2 with one command:

git submodule update

Now you are able to refactor code1.js without import errors.

Submodules are just references to certain commits. So when you want to update libraries to new versions, you have to update the references. You can do it by the below command.

git submodule update --remote

Now you can see how useful it is to only initialize the submodules you need.

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.