Submodules in Git (original) (raw)

Last Updated : 7 Apr, 2026

Git submodules allow one repository to reference another at a specific commit, enabling dependency management without merging codebases.

Benefits of Using Git Submodules

Git submodules help you manage external repositories in a structured, controlled, and scalable way within your main project.

Use Cases for Git Submodules

Git submodules are especially useful when projects require shared codebases or independent version management.

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

Running git status shows the .gitmodules file and the submodules-in-git directory. You can then inspect the contents of the .gitmodules file as shown in the terminal output:

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

This command copies submodule configuration from the .gitmodules file to the local .gitconfig, enabling selective submodule usage.

Using git submodule init

Workflows in Submodules

After initialization, a submodule works as an independent Git repository within the parent project.

Adding and Committing files in the submodule

After making changes in a submodule, the parent repository detects those updates and must be committed to record the new submodule state.

Using git status command in the parent repository