Class 27 of 50 • Remote Collaboration

Git Submodules & Subtrees: Managing Nested Repositories & Shared Libraries

In large enterprise projects, software applications rarely exist in isolation. You might build an e-commerce platform that shares a common payment library, custom UI components, or encryption module with five other internal applications. Git Submodules let you keep a Git repository as a subdirectory of another Git repository while preserving separate commit histories.

Adding a Submodule to a Project

To include an external repository as a submodule inside your project:

# Add a submodule into the 'libs/payment-sdk' folder:
$ git submodule add https://github.com/company/payment-sdk.git libs/payment-sdk

When you run this command, Git creates two critical artifacts:

  1. .gitmodules file: A plain text configuration mapping the submodule's local path to its remote repository URL:
    [submodule "libs/payment-sdk"]
        path = libs/payment-sdk
        url = https://github.com/company/payment-sdk.git
  2. A gitlink entry: Git does not track the submodule's individual files in the main repository's tree. Instead, it records a special 160000 mode entry pointing to a single commit hash of the submodule.

Cloning a Repository with Submodules

If you run a standard git clone on a repository containing submodules, the submodule directories will be cloned as empty folders! To initialize and download their contents:

# Recommended: Clone and initialize all submodules in one command:
$ git clone --recurse-submodules https://github.com/company/main-app.git

# If you already cloned normally without the flag, run:
$ git submodule update --init --recursive

Updating Submodules to Latest Remote Commits

Because submodules are pinned to exact commit hashes, they do not update automatically when new commits are pushed upstream. To update them:

# Fetch and merge new commits for all submodules:
$ git submodule update --remote --merge

# Commit the updated commit pointer in the parent repository:
$ git commit -am "chore: update payment-sdk submodule to latest release"

Submodules vs Git Subtree

While submodules store external links, Git Subtree merges the external project's files and commit history directly into your parent repository's tree. Subtrees avoid empty folder issues for colleagues, but require more complex merge commands.

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Git Internals & Tree Objects

Understand how submodules record commit SHA-1 pointers inside parent trees.

Remote Repositories Management

Clone nested submodules recursively using git clone --recurse-submodules.

Monorepo vs Polyrepo Workflows

Evaluate submodules, subtrees, and monorepo architectures for enterprise codebases.

Hands-on Practice Lab Scenarios

Add, update, initialize, and remove submodules in real projects.

Frequently Asked Questions (FAQ) - Student & Interview Review

If you navigated into the submodule directory and made uncommitted changes or checked out a different branch, the parent repository detects that the submodule's HEAD differs from the pinned commit.
Yes. Enter the submodule directory (`cd libs/payment-sdk`), switch to a branch, make your edits, commit, and push to the submodule's remote. Then return to the parent directory and commit the new submodule pointer.
De-initialize the submodule (`git submodule deinit -f path/to/submodule`), remove it from the Git index (`git rm -f path/to/submodule`), and remove the remaining `.git/modules/path/to/submodule` directory.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to GitHub Tour & Ecosystem → Previous: ← PAT & Credentials | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!