Nesting Repositories with Git Submodules: A Newbie's Guide
Introduction¶
Are you facing the challenge of handling multiple code pieces scattered across different repositories in your project, unsure how to seamlessly integrate them?
For developers new to the concept, managing disparate repositories within a single project can be overwhelming. Git submodules offer a guiding light, acting as a map through the maze of organizing and linking these separate codebases or libraries within your projects.
Real-Life Scenario: Aligning Frontend and Backend Strategies¶
Back in 2022, I found myself as the lead developer overseeing the backend team, while collaborating closely with a talented frontend developer responsible for crafting engaging user interfaces.
Our teams operated independently, each excelling in our specialized domains. However, this independence led to distinct branch strategies. The backend team adopted a unique approach, separate from the frontend's strategy.
Over time, this divergence in branch strategies caused disparities between our repositories' states. Aligning frontend changes with the evolving backend structures became a complex task. Ensuring seamless integration between our frontend branches and specific backend versions posed challenges.
Recognizing these challenges, I engaged in a discussion with the frontend developer. We brainstormed solutions to synchronize versions and segregate our branch strategies effectively.
During our deliberation, we explored the idea of utilizing Git submodules. It wasn't merely about syncing versions but aligning our separate branch strategies while maintaining distinct team autonomy.
The proposal envisioned Git submodules as the bridge between our frontend and backend repositories, facilitating version synchronization and accommodating separate yet aligned branch strategies. This approach aimed to streamline collaboration and ensure smoother integration between our teams' work.
Motivated by the vision of enhanced collaboration and harmonized branch strategies, we collectively agreed to integrate Git submodules. This decision promised a more cohesive development environment, allowing both teams to synchronize versions and align branch strategies seamlessly.
Extending to a Computer Vision Project¶
Additionally, in a computer vision project, I encountered a similar challenge. Testing code from various repositories required frequent modifications, causing inefficiencies. Managing these disparate codebases led me to prefer a unified repository managed with Git submodules. This approach enabled me to centralize and manage all required codebases efficiently, adapting them as needed.
Think of Git submodules as containers that neatly organize and link external repositories to your main project—providing a solution to the discomfort of handling disjointed pieces of code. Join us as we embark on a journey to explore how to clone, set up, and effortlessly synchronize these submodules within your projects.
This document simplifies Git submodules in a beginner-friendly way, offering developers new to the concept a clear path to effectively manage multiple repositories as cohesive parts of their projects.
Cloning a Repository with Submodules and Cloning a Specific Submodule¶
Clone the Main Repository¶
-
Open your terminal and navigate to the desired directory for cloning:
-
Clone the main repository:
Replace
<repository_url>with the URL of the main repository. -
Change your working directory to the repository:
Replace
<repository_directory>with the name of the cloned directory.
Initialize and Update Submodules¶
-
Initialize the submodules:
This sets up necessary Git configurations for submodules.
-
Update the submodules:
This fetches submodule contents based on references in the main repository.
Clone a Specific Submodule¶
-
Clone a specific submodule:
Replace
<submodule_path>with the specific submodule path. This command updates only the specified submodule and its dependencies, leaving others unchanged. - The--recursiveflag initializes nested submodules within the specified submodule.
Now that you've successfully cloned the main repository along with its submodules, let's explore how to create and manage submodules within an existing repository.
If the update fails, you may want to read this stackoverflow thread
Creating a Git Submodule¶
-
Move to the parent repository's root directory:
-
Add the Submodule:
<submodule_repository_url>: URL of the submodule repository.<submodule_path>: Path within the parent repository to place the submodule.
Example:
-
Commit the Changes:
Replace
<submodule_path>with the actual path used when adding the submodule. -
Push Changes (Optional):
Now, let's delve into pulling changes from both the main repository and its submodules to keep your local copy up to date.
Pulling Changes from the Main Repository and Submodules¶
Update the Main Repository¶
-
Navigate to the main repository's directory:
-
Fetch the latest changes:
This command fetches and merges the latest changes from the remote repository into your local
mainbranch.
Update Submodules¶
-
Update submodules to the latest commits:
This updates each submodule to the commit specified by the main repository.
-
Update a specific submodule:
-
Using
git submodule update --remote <submodule_path>: -
Or manually in the submodule directory:
-
Pushing Updated Submodule References (Bonus)¶
-
Inside the main repository, after updating submodule references:
-
If there are changes in the submodules themselves:
Removing a Git Submodule¶
To remove a submodule from your repository, follow these steps. The following instructions are based on a detailed explanation found on Stack Overflow (source).
-
Move to the parent repository's root directory:
Before removing the submodule, move it temporarily to a different location within your working directory.
-
Deinitialize the Submodule: Use the following command to deinitialize the submodule:
-
Remove Submodule Configuration: Delete the submodule's configuration from the
.git/modulesdirectory: -
Remove Submodule from Repository: There are two options to remove the submodule from the repository:
a. If you want to completely remove it from the repository and your working tree:
Note: Replace
<submodule_path>with the actual submodule path.b. If you want to keep it in your working tree but remove it from the repository:
-
Restore Submodule (Optional): If you moved the submodule in step 0, restore it to its original location:
By following these steps, you'll effortlessly manage main repositories and their submodules, ensuring your projects are up to date.
Stay tuned for more Git tips and tricks on our blog for seamless collaboration and version control!