Module 7 Lesson 1: What are submodules and why use them
·DevOps

Module 7 Lesson 1: What are submodules and why use them

Master project architecture. Learn how Git Submodules allow you to keep one repository as a subdirectory of another while maintaining independent version control histories.

Module 7 Lesson 1: What are submodules and why use them

As projects grow, they often depend on other projects.

  • Maybe you are building a website and want to use a specific, custom CSS framework you wrote.
  • Maybe you are building a game and need to include an external physics engine.

You could just copy-paste the code into your project, but then you lose the ability to track updates to that external project. You could use a package manager (like npm), but that’s not always suitable for code you are actively developing yourself.

This is where Git Submodules come in.


1. Defining a Submodule

A Git submodule is a record within a "Parent" repository that points to a specific commit in a "Child" repository.

  • It allows you to keep another project in a subdirectory of your project.
  • The child repository has its own independent history, commits, and branches.
  • The parent repository doesn't store the child’s files; it only stores the child's URL and the SHA-1 ID of the commit the parent is currently using.

2. Why use them?

Separation of Concerns

You can keep a library clean and generic in its own repository while using it across multiple different products.

Control over Versions

Unlike a package manager that might automatically update and break your build, a submodule only changes when you explicitly tell Git to update to a new commit ID.

Development Speed

If you own both the parent and the child repo, you can make changes in the library and immediately see them in your app without waiting for an npm publish cycle.

graph TD
    Parent["Parent Repository (Main App)"] --> Lib1["Submodule 1 (Auth Lib)"]
    Parent --> Lib2["Submodule 2 (UI Kit)"]
    
    Lib1 -- "Independent History" --> C_A["Commit A"]
    Lib2 -- "Independent History" --> C_B["Commit B"]

3. The Downside

Submodules are powerful but tricky.

  • They require extra commands to initialize and update.
  • They can lead to "Headless" states if not managed carefully.
  • They make the git clone process more complex for your colleagues.

In the rest of this module, we will learn how to navigate these complexities safely.


Lesson Exercise

Goal: Identifying Dependency Patterns.

  1. Think about a project you are working on.
  2. Does it use any libraries that you also help develop?
  3. If not, find a large open-source project (like TensorFlow or Unreal Engine) on GitHub.
  4. Check their file list for a .gitmodules file.
  5. Open that file and see how many external projects they are pulling in using submodules.

Observation: You'll see that large, professional projects almost always use submodules to manage their internal and external architectural dependencies.


Summary

In this lesson, we established:

  • Submodules are repositories inside repositories.
  • They allow for independent version control of shared libraries.
  • They provide granular control over which version of a dependency is used.
  • They come with a steeper learning curve than standard Git usage.

Next Lesson: We’ll learn how to actually Add a submodule to your project and see what files Git creates to track it.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn