Open In App

Submodules in Git

Last Updated : 17 Nov, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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


Suggested Quiz
5 Questions

What is a Git submodule?

  • A

    A copy of the repository with no history

  • B

    A pointer to another repository at a specific commit

  • C

    A temporary branch linked to remote

  • D

    A merged repository inside another repo

Explanation:

Submodules are linked external repositories tracked at specific commit hashes, allowing modular project structure.

Which command is used to add a submodule to a repository?

  • A

    git submodule create <url>

  • B

    git add submodule <url>

  • C

    git submodule add <url>

  • D

    git init submodule <url>

Explanation:

git submodule add <URL> clones and links a repository as a submodule inside the main repository.


What file gets created automatically when a submodule is added?

  • A

    .gitignore

  • B

    .gitmodules

  • C

    subrepo

  • D

    .moduleconfig

Explanation:

.gitmodules stores details of submodule paths and URLs, letting Git track and manage them.

How do you clone a repository along with its submodules?

  • A

    git clone <url>

  • B

    git clone --with-submodules <url>

  • C

    git submodule clone <url>

  • D

    git clone --recursive <url>

Explanation:

git clone --recursive <url> clones the main repo and automatically initializes all submodules.

What does git submodule init do?

  • A

    Deletes submodule refs

  • B

    Copies submodule config from .gitmodules to Git environment

  • C

    Commits all submodule changes automatically

  • D

    Updates the parent repository

Explanation:

git submodule init registers submodules from .gitmodules so they can be initialized/updated selectively.

Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore