Cspsprotocol

git submodule

What is a submodule in Git? How to add, update, and remove it?


While using Git for source code version control, you might come to a situation where a project includes multiple subprojects. Where each project can be another Git repository itself. However, they are all under the same top-level directory structure.

For example, the main application includes code to connect to a remote machine at a specific IP address via different types of network sockets. For each type, there are subfolders, e.g  UDP, TCP, and SCTP socket wrappers.

Additionally, a linked subproject can be hosted at a different git repository. The code may include third-party libraries that must be included in your project, as well as other useful code.


This tutorial will discuss what a submodule is and how to add, remove, and update submodules in your working project.

What is a submodule in Git?

Submodules in Git are convenient for referencing other remote projects from your development pipeline. You can clone all the referenced submodules while cloning the main project. This eliminates the manual work required to keep all third-party software up to date.

To demonstrate the submodule concept here, we are considering two git repositories, one for our project and another for a third-party library. The following examples demonstrate how to add, clone, update, and remove a third-party submodule in the main project using Git commands.

Project URL
Main Project https://github.com/cspsprotocol/projectsource.git
Third-Party https://github.com/cspsprotocol/third-party.git

Git submodule add.

The git submodule add command adds a module to the repository. It requires two arguments: the URL and the folder name (optional) containing the code of the submodule. In the absence of a folder name, the repository’s name will be used.

git submodule add https://github.com/cspsprotocol/third-party.git
Cloning into 'third-party'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

Submodules are added just like other resources. We need to commit and push for future usage.

 git commit -m" Added submodule third-party"
 git push

Git submodule clone.

Submodule code is not automatically copied when cloning a repository. However, empty folders are created for each submodule. You may use the option to fetch submodules’ code either at the time of cloning or later.

Use a -recurse-submodules Option with the clone command to bring the code of submodules.

git clone --recurse-submodules  https://github.com/cspsprotocol/projectsource.git

Git submodule update.

Clone submodules later, using the git submodule update command with -recursive and -init command-line options.

git submodule update  --recursive --init

Git submodule remove.

Removing a submodule in Git involves the following steps.

  • Deinit the submodule (git submodule deinit <submodule-name>) – This is the first step that will remove the references (in .git/config file) of the submodule given in the command line.
  • Remove the folder – Use Linux delete directory command (rm). This will delete all files inside the folder along with the folder itself.
  • Remove the directory for the next commit.
  • Commit the changes.
  • Push the code (git push command) to update the central repository.
git submodule deinit third-party
Cleared directory 'third-party'
Submodule 'third-party' (https://github.com/cspsprotocol/third-party.git) unregistered for path 'third-party'
rm -rf third-party/
git rm third-party
 
git commit -m"Removed the submodule third-party"
[master 0ad6683] Removed the submodule third-party
 Committer: root <root@localhost.localdomain>
git push

Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 234 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/cspsprotocol/projectsource.git
   f1ed01e..0ad6683  master -> master



Scroll to Top