DEV Community

Cover image for Utilizing Git Submodules for Including a Separate Repository
Lydia Yuan
Lydia Yuan

Posted on • Edited on

Utilizing Git Submodules for Including a Separate Repository

As developers, we often find ourselves in situations where we want to include a separate repository within another repository. Git submodules provide a helpful feature to achieve this goal. In this blog post, we'll explore how to set up Git submodules to include a separate repository within another repository.

Setting Up Git Submodules

To get started, follow these steps:

Step 1: Create the Separate Repository

Begin by creating the separate repository that you want to include within another repository.

Step 2: Navigate to the Target Repository

Next, navigate to the repository where you want to include the separate repository.

Step 3: Add the Separate Repository as a Submodule

In the target repository's folder, run the following command to add the separate repository as a submodule:

git submodule add <separate-repo-url> <submodule-folder-name>
Enter fullscreen mode Exit fullscreen mode

Make sure to replace <separate-repo-url> with the URL of the separate repository and <submodule-folder-name> with the desired name for the submodule folder.

Step 4: Commit the Changes

Step 5: Push Changes to the Remote Repository

Benefits of Git Submodules

By following these steps, whenever someone clones the target repository, the separate repository will be included as a submodule. This setup allows you to update, commit, and push changes to the separate repository independently from the target repository. It enables you to maintain separate commit histories and manage the two repositories effectively.

Cloning the Repository on Another Machine

It's important to note that when cloning the target repository on another machine, additional steps are required to fetch and initialize the submodule. After cloning the repository, run the following commands:

git submodule init
git submodule update --remote
Enter fullscreen mode Exit fullscreen mode

These commands will fetch the separate repository and initialize it within the target repository on the new machine.

I hope you find this guide helpful for incorporating Git submodules into your projects โœ(โ—”โ—กโ—”)

Top comments (0)