![]() |
VOOZH | about |
Git submodules allow you to keep a Git repository as a subdirectory of another Git repository. This is useful when you want to include external libraries or shared components within your project while maintaining their history and keeping them separate from your main repository.
In this article, we will walk you through everything you need to know to use Git submodules effectively.
Table of Content
A Git submodule is a repository embedded inside another Git repository. The main repository, known as the superproject, tracks the submodule's state via a specific commit hash. This allows the superproject to reference a particular version of the submodule, making it easy to include third-party libraries or shared codebases as part of your project while maintaining separation between them.
Git submodules are useful for:
To add a submodule to your repository:
Open your terminal and navigate to your Git repository.
git submodule add <repository-url> <path>Replace <repository-url> with the URL of the submodule repository, and <path> with the directory where the submodule should be added.
git submodule update --initThis command initializes and fetches the submodule data.
When cloning a repository that contains submodules, use the --recurse-submodules flag:
git clone --recurse-submodules <repository-url>This command clones the main repository and initializes all submodules.
To update submodules to their latest commit on the specified branch or tag:
git submodule update --remoteThis fetches the latest changes for all submodules.
Enter the submodule directory:
cd <submodule-path>git add .
git commit -m "Update submodule content"
Push the changes to the submodule’s remote repository:
git pushGo back to the main repository and commit the updated submodule reference:
cd..
git add <submodule-path>
git commit -m "Update submodule reference"
To pull the latest changes for a submodule:
cd <submodule-path>git pull origin <branch>cd ..
git add <submodule-path>
git commit -m "Update submodule to latest version"
To remove a submodule:
git submodule deinit -f -- <submodule-path>rm -rf .git/modules/<submodule-path>Edit the `.gitmodules` file to remove the submodule entry, and commit the change:
git rm -f <submodule-path>By default, submodules are pinned to specific commits. To track a branch:
cd <submodule-path>
git checkout <branch>
git submodule update --remote --mergeSubmodules can themselves contain submodules, known as nested submodules. Use the `--recursive` flag when updating or cloning to include nested submodules:
git submodule update --init --recursiveIn CI/CD environments, ensure submodules are properly initialized by adding submodule update commands in your pipeline configuration files:
git submodule update --init --recursiveSubmodules often operate in a detached HEAD state. To switch to a branch:
git checkout <branch>Remember to commit and push the changes if you make updates within the submodule.
Conflicts can occur if submodule updates overlap. Resolve them by checking out the correct commit in the submodule and committing the resolution in the main repository.
If a submodule directory is missing after cloning:
git submodule initgit submodule updategit submodule update --remoteEnsure you test updates thoroughly to avoid breaking changes.