DEV Community

Cover image for Git Submodule: Managing Dependencies with Precision
Milad Roudgarian
Milad Roudgarian

Posted on

Git Submodule: Managing Dependencies with Precision

Assume that many files are shared between countable microservices and are crucial for the project life-cycle. Such as the .proto file that could be used in both the gateway service and many other related services.
As an approach, you can use a symbolic link to have access, but many conflicts like dependency on the file system structure occur. If directories change or if you move your project, links might break. Consider using Git submodules or separate Git repositories for shared proto files provides versioning and easier management for this dummy scenario.

What is the Git Submodule?

Git submodules facilitate the inclusion of one Git Repository as a Subdirectory within another Git Repository.
Essentially, a Git submodule is a reference to a specific snapshot of another repository. This functionality
allows a Git repository to integrate external code, retaining the ability to track its version history.

Git submodules are beneficial in scenarios where strict version control of external dependencies is crucial. Common use cases include:

  • Ensuring Stability: If an external component evolves rapidly or anticipated changes may disrupt the API, you can secure the code by fixing it to a specific commit for stability.

  • Managing Vendor Dependencies: For components with infrequent updates, Git submodules are useful for tracking them as vendor dependencies.

  • Third-Party Integration: Delegating a project segment to a third party becomes more manageable when you want to incorporate their work at a specific time or release, especially when updates are sporadic.

Git Submodule Practical Commands

These are basic commands, and depending on your specific use case, you might need to explore more advanced submodule commands.
To dig deeper into the concept, reviewing the Git Official Documentation is recommended.

  • Add a Submodule:
git submodule add <repository_URL> <path>
Enter fullscreen mode Exit fullscreen mode

Example:

git submodule add https://github.com/example/repo.git vendor/repo
Enter fullscreen mode Exit fullscreen mode
  • Initialize Submodules After Cloning a Repository:
git submodule update --init --recursive
Enter fullscreen mode Exit fullscreen mode
  • Update Submodules: To fetch the latest changes from the submodule repositories.
git submodule update --remote
Enter fullscreen mode Exit fullscreen mode
  • Clone a Repository with Submodules: To clone a repository and its submodules.
git clone --recursive <repository_URL>
Enter fullscreen mode Exit fullscreen mode
  • Initialize Submodules After Cloning (Older Git Versions): For Git versions older than 1.6.5.
git submodule init
Enter fullscreen mode Exit fullscreen mode
git submodule update
Enter fullscreen mode Exit fullscreen mode
  • Fetch Submodule Changes: To pull changes from the master branch of each submodule.
git submodule deinit -f -- <submodule_path>
Enter fullscreen mode Exit fullscreen mode
git rm -f <submodule_path>
Enter fullscreen mode Exit fullscreen mode
rm -rf .git/modules/<submodule_path>
Enter fullscreen mode Exit fullscreen mode
  • Remove a Submodule: After running these commands, you also need to commit the changes.
git submodule deinit -f -- <submodule_path>
Enter fullscreen mode Exit fullscreen mode
git rm -f <submodule_path>
Enter fullscreen mode Exit fullscreen mode
rm -rf .git/modules/<submodule_path>
Enter fullscreen mode Exit fullscreen mode
  • Update Submodule URL: If the URL of a submodule has changed, you need to run this command to update the configuration.
git submodule sync
Enter fullscreen mode Exit fullscreen mode
  • Clone Submodule Recursively: To clone a repository and its submodules in one step.
git clone --recursive <repository_URL>
Enter fullscreen mode Exit fullscreen mode

Custom Commands
You can use the git submodule foreach command to run custom commands on each submodule.

Example:

git submodule foreach 'git checkout master'
Enter fullscreen mode Exit fullscreen mode

This checks out the master branch in each submodule.

Top comments (0)