Trusted answers to developer questions

How to use Git submodules

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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

  1. Easy separation of code into different repositories.
  2. 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.

RELATED TAGS

git

CONTRIBUTOR

Anjana Shankar
Attributions:
  1. undefined by undefined
Did you find this helpful?