Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between submodule add and init?

Tags:

git

Really, what is the difference between submodule add and submodule init? Or more precisely when would you want to use one without the other?

like image 554
matanster Avatar asked Jul 21 '26 16:07

matanster


2 Answers

You use git submodule add when you yourself want to add a new submodule to a project.

You use git submodule init when you clone a project that has submodules, or when someone else has added a submodule to a project.

like image 131
1615903 Avatar answered Jul 25 '26 00:07

1615903


Add is when you want to import another git repository to your current git repository (called the "superproject"). To perform that it records in the .gitmodules the path to the repository, thus adding it to the index.

Submodule init will create already-existing submodules recorded in the index. This is achieved configuring the paths found in the index in git.

Add:

Add the given repository as a submodule at the given path to the changeset to be committed next to the current project: the current project is termed the "superproject".

Init:

Initialize the submodules recorded in the index (which were added and committed elsewhere) by copying submodule names and urls from .gitmodules to .git/config.

Therefore init checks out the submodules already recorded in the index that where previously added with

git submodule add my_sub_project directory_in_superproject

Typically when cloning the superproject you would do:

git clone superproj

within the project then:

git submodule update --init
like image 39
g24l Avatar answered Jul 25 '26 00:07

g24l



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!