Understanding Git Submodules

Understanding Git Submodules

Git submodules are an essential feature of Git that allow you to include one Git repository as a subdirectory in another. This feature is especially useful when you need to share code between projects or when a project depends on another repository. In this article, we’ll explore what Git submodules are, their use cases, how to set them up, and best practices to ensure smooth integration in your projects.


What is a Git Submodule?

A Git submodule is a pointer to a specific commit in another repository. It allows you to include a separate repository as a subdirectory within your main project while maintaining a clear boundary between the two. This means changes to the submodule repository are not directly tracked in the parent repository but are referenced by a specific commit ID. Actually its a full and separate git repository itself with its own commit history.

Git Submodules allow you to share two or more repositories as though they were one. Each repository maintains its own seperate change history and submodules are updated independently of the main repository. When you clone or pull a repository with a submodule, the repository has a link to where to get the submodule code from.


Use Cases for Git Submodules

  1. Code Reusability: Sharing common libraries or modules across multiple projects.
  2. Third-Party Dependencies: Integrating third-party repositories directly into your project.
  3. Modular Development: Working on different parts of a project as separate repositories for better organization and team collaboration.

Submodules are useful if you have a code or content in one git repository that you want to use with several other git managed projects, yet you still want to keep the change history seperate. For example, you may be using a library that is under active development and you need to develop you code along with any changes.


Setting Up Git Submodules

Adding a Submodule

To start using git submodules you first need a Git repository, this can be a new repository or an existing one. Lets call this the main repository. In the root of the main repository, you add a submodule using the git submodule add command as follows:

git submodule  add -b <branch> --name <name>  <repository>
git submodule add <repository-url> <sub-directory-name>        

This command does the following:

  • Clones the submodule repository into the specified directory.
  • Adds a .gitmodules file in the parent repository to track the submodule

Cloning a Repository with Submodules

When you clone a repository containing submodules, you also need to initialize and update them:

git clone <repository_url>
cd <repository>
git submodule update --init --recursive        

This ensures all submodules are fetched and checked out.

Updating Submodules

To pull the latest changes from the submodule repository:

cd <path_to_submodule>
git pull origin <branch>        

After pulling the updates, commit the updated submodule reference in the parent repository:

git add <path_to_submodule>
git commit -m "Update submodule"        

Removing a Submodule

To remove a submodule:

  1. Remove the submodule entry from .gitmodules.
  2. Delete the submodule directory.
  3. Unstage the submodule from the index
  4. Commit the changes.

git rm --cached <path_to_submodule>        

Best Practices for Git Submodules

  1. Pin Submodules to Specific Commits: Always reference a stable commit in the submodule to avoid unexpected changes.
  2. Document Submodule Usage: Include instructions in the README.md for setting up and updating submodules.
  3. Use Submodules for Truly Independent Repositories: Avoid submodules for tightly coupled projects; instead, consider using a mono repo structure.
  4. Automate Updates: Use scripts to simplify the process of updating and initializing submodules for your team.


Common Pitfalls and How to Avoid Them

  1. Forgetting to Update Submodules: Always update submodules before building or deploying your project.
  2. Merge Conflicts in Submodules: Resolve conflicts carefully by checking out the correct commit in the submodule.
  3. Overusing Submodules: Don’t use submodules if the dependency is small or changes frequently—consider copying the files instead.


Git Submodules are a great way to distribute several repositories all as one. Each Submodule should be treated as a completely seperate repository to get the most sence out of using Git Submodules. Take the time to learn how to use Submodules and you will find them easy to use and very helpful in the right situations.

To view or add a comment, sign in

More articles by Priyanka Sain

Insights from the community

Others also viewed

Explore topics