Examples
Git Submodule Project
Using a Submodule
Git submodule project includes a nested repo with git submodule add.
Introduction to Git Submodules
Git submodules allow you to include and manage a Git repository as a subdirectory of another Git repository. This is particularly useful when dealing with dependencies or shared code across projects. By using submodules, you can track the history of the subproject independently while keeping it tied to your main project.
Adding a Submodule
To add a submodule to your project, you use the git submodule add command. This command clones the repository into a subdirectory of your project and adds an entry to the .gitmodules file.
In this example, replace https://github.com/example/repo.git with the URL of the repository you wish to add and path/to/submodule with the desired path in your project structure.
Cloning a Repository with Submodules
When cloning a repository that contains submodules, you need to initialize and update the submodules to pull in their content. Use the following commands after cloning:
Updating Submodules
Submodules do not automatically update when you pull changes from the main repository. To update a submodule to the latest commit, navigate to the submodule's directory and pull the changes:
Replace main with the appropriate branch if necessary.
Committing Changes in Submodules
Changes made within a submodule are independent of the main project. If you make changes in a submodule, commit them from within the submodule's directory. Then, return to the main project and commit the updated submodule reference:
Removing a Submodule
To remove a submodule, you need to perform several steps manually:
- Remove the submodule entry from the
.gitmodulesfile. - Run
git rm --cached path/to/submoduleto remove the submodule from the index. - Delete the submodule's directory from the filesystem.
- Commit the changes.
Examples
- Previous
- Pull Request
- Next
- Monorepo Setup