How to use Git submodules
Git submodules are a powerful way to use git as an external dependency management tool. It is basically a way to embed a repository into another. When you add a submodule in Git, the code of the submodule does not get added to the main repository (only certain information about the submodule does); instead, it simply adds a reference to the submodule. This is analogous to the soft link you may have created in your file system.
Advantages of using submodules
- Easy separation of code into different repositories.
- The submodule can be added into multiple repositories, allowing for easy management.
Adding a submodule
First, change the directory to the git repository that you want to add the submodule.
git submodule add <submodule_repo_path>
When you do,
git status
you will see that a .gitmodules file has been added, along with the directory corresponding to your git submodule repository.
The .gitmodules file contents would be look like:
[submodule "<submodule_name>"]
path = <submodule_directory>
url = <submodule_repo_path
Now you can,
git add
these two and push it to your remote repo.
On the remote side, you should see a symlink from this repository to the submodule repository.
Free Resources
- undefined by undefined