Understanding Git Submodules
Git submodules are an essential feature of Git that allow you to include one Git repository as a subdirectory in another. This feature is especially useful when you need to share code between projects or when a project depends on another repository. In this article, we’ll explore what Git submodules are, their use cases, how to set them up, and best practices to ensure smooth integration in your projects.
What is a Git Submodule?
A Git submodule is a pointer to a specific commit in another repository. It allows you to include a separate repository as a subdirectory within your main project while maintaining a clear boundary between the two. This means changes to the submodule repository are not directly tracked in the parent repository but are referenced by a specific commit ID. Actually its a full and separate git repository itself with its own commit history.
Git Submodules allow you to share two or more repositories as though they were one. Each repository maintains its own seperate change history and submodules are updated independently of the main repository. When you clone or pull a repository with a submodule, the repository has a link to where to get the submodule code from.
Use Cases for Git Submodules
Submodules are useful if you have a code or content in one git repository that you want to use with several other git managed projects, yet you still want to keep the change history seperate. For example, you may be using a library that is under active development and you need to develop you code along with any changes.
Setting Up Git Submodules
Adding a Submodule
To start using git submodules you first need a Git repository, this can be a new repository or an existing one. Lets call this the main repository. In the root of the main repository, you add a submodule using the git submodule add command as follows:
git submodule add -b <branch> --name <name> <repository>
git submodule add <repository-url> <sub-directory-name>
This command does the following:
Cloning a Repository with Submodules
When you clone a repository containing submodules, you also need to initialize and update them:
Recommended by LinkedIn
git clone <repository_url>
cd <repository>
git submodule update --init --recursive
This ensures all submodules are fetched and checked out.
Updating Submodules
To pull the latest changes from the submodule repository:
cd <path_to_submodule>
git pull origin <branch>
After pulling the updates, commit the updated submodule reference in the parent repository:
git add <path_to_submodule>
git commit -m "Update submodule"
Removing a Submodule
To remove a submodule:
git rm --cached <path_to_submodule>
Best Practices for Git Submodules
Common Pitfalls and How to Avoid Them
Git Submodules are a great way to distribute several repositories all as one. Each Submodule should be treated as a completely seperate repository to get the most sence out of using Git Submodules. Take the time to learn how to use Submodules and you will find them easy to use and very helpful in the right situations.