Dipu Dey 17th Oct 2022

Git submodule is a way to reuse code across multiple projects. It is usually code embedded with the main repository. The submodule repository code doesn’t add the main repository code.

Sometimes you don’t work with the latest submodule code, so if you are using submodules, your submodule’s code will not update automatically if you don’t want it to.

Add Submodule to Repository

<code class="language-php">
git submodule add repository_link your_directory
</code>

Example:

<code class="language-php">
git submodule add https://github.com/codeboxrcodehub/pathao-courier.git couriercompany
</code>

After running this command a new file is added to your root directory called .gitmodules and pulls your repository code. This file is a list of all submodule lists and repository lists.

Update Submodule in Local Repository

Submodules do not update automatically. so if you want to update the code then run.

<code class="language-php">
git submodule update --recursive --remote
</code>

If it’s the first time you check out a repository you need to use

<code class="language-php">
git submodule update --init
</code>

If you change or add any code in a submodule the main repository can’t track your changes and commit. The module changes are considered “dirty”. So you need to step into the submodule and commit and push your changed code.