Lesson 50 +20 XP

Git Submodules

Git Submodules

A submodule is a Git repository nested inside another Git repository. It lets you include another project without copying its files.

Why use submodules?

  • Include a shared library in several projects.
  • Keep a third party project at a specific version.
  • Track a dependency as part of your own history.

Add a submodule

git submodule add https://github.com/user/lib.git lib

This clones the repo into the lib folder and records its commit.

What Git stores

Git does not store the submodule's files. It stores a reference to the commit the submodule is on. The submodule's own repository keeps the code.

Clone a project with submodules

git clone URL
git submodule update --init

After cloning, submodules are empty until you run the update command.

Update a submodule

git submodule update --remote

This pulls the latest commit from the submodule's remote.

The trade off

Submodules are powerful but add complexity. Many projects prefer package managers (npm, pip) instead.

TL;DR

  • A submodule is a repo nested inside another repo.
  • git submodule add URL folder adds one.
  • Clones need git submodule update --init.
  • Git records the submodule's commit, not its files.