Submodules in Git

In software development, it’s common to manage projects that depend on other projects. Git submodules are a powerful feature that allows you to include and manage external repositories within your main repository. This feature enables you to keep multiple repositories in sync and makes it easier to work on complex projects that rely on various codebases. This article will guide you through the concept of Git submodules, their benefits, and how to use them effectively.

What Are Git Submodules?

Git submodules are pointers to another repository at a specific commit. They allow you to include a separate repository within your main repository, maintaining a connection between the two. This means you can manage dependencies as part of your project without merging all the code into a single repository.

Benefits of Using Git Submodules

  • Modularity: Keep your project modular by separating dependencies into distinct repositories.
  • Isolation: Manage changes in external dependencies without directly affecting your main project.
  • Version Control: Pin dependencies to specific versions, ensuring stability and consistency.
  • Reusability: Reuse submodules across multiple projects, avoiding code duplication.

Use Cases for Git Submodules

  • Large Projects: Split large projects into manageable components.
  • Shared Libraries: Include common libraries shared across different projects.
  • Third-Party Dependencies: Manage third-party code dependencies separately from your main codebase.

Adding Git submodules

Adding submodules in git

Here, we can see to add a submodule in the git repository we have used the command:-

git submodule add url_of_the_git_repository

The above command takes URL as a parameter that points to a repository on Github. Here we have added the 
Submodules-in-git as a submodule. This command will clone the submodule and after that, we can check the status of the git repository using the git status command.

Here we can see that using the below command  git status as it shows us the two files that are .gitmodules and the submodules-in-git directory. Now let’s see the contents of the .gitmodules file. These commands are as depicted via terminal/ PowerShell in below media as follows:

Contents of .gitmodules

Cloning a GitHub repository having  .gitmodules

For Cloning the repository having submodules we have to use the following command:  

git clone ---recursive url_of_the_github_repository

Cloning GitHub repository having submodules

Git init

It is used to copy the mappings from a  .gitmodules file to a ./.gitconfig file.  Moreover, it is very helpful when there are many
submodules present in a repository activate only specific submodules which are needed for working in the repository.

Using git submodule init 

Workflows in submodules

As soon as you initialize the submodules and update them within a parent repository now we can use them as a different repository that can have their own branches and history. So when we are creating some changes in the submodule it’s important to have a track on them and commit them properly. So let’s switch to our submodule here  Submodules-in-git

Adding and Committing files in the submodule

Now let’s move back to the parent repository of this submodule which is present in folder10 and let’s see the status of the parent repository. As we can see that now the parent repository is aware that some changes are made in the submodules  Now if one wants to update this parent repository you can do so by using git add and git commit commands.

Using git status command in the parent repository