What Are Git Submodules and How Do They Work?

A Git submodule embeds one Git repository inside another as a subdirectory, pinned to a specific commit.

Published September 28, 2026

A Git submodule embeds one repository inside another at a specific commit, so a project can depend on external code while keeping its history separate.

How it works

The parent repo stores a pointer (commit hash) to the submodule, plus its URL in .gitmodules. Cloning needs --recurse-submodules or a follow-up init/update.

Updating means moving the submodule to a new commit and committing that pointer in the parent.

Try it

git submodule add https://github.com/org/lib.git libs/lib
git submodule update --init --recursive
Submodule path 'libs/lib': checked out 'a1b2c3'

Practical examples

Add

git submodule add https://github.com/org/lib.git libs/lib

Clone with submodules

git clone --recurse-submodules <url>

Update to latest remote

git submodule update --remote

Remove

git submodule deinit -f libs/lib
git rm -f libs/lib

Common mistakes

  • Forgetting to init submodules after cloning.
  • Committing inside a submodule but not pushing it.
  • Not committing the updated pointer in the parent.

Key takeaways

  • Submodules pin another repo at a commit.
  • Clone with --recurse-submodules.
  • Commit the pointer after updating.

FAQ

Submodule vs package manager?

Use a package manager for libraries when possible; submodules suit vendored source.

Why is my submodule empty?

Run git submodule update --init.

More Git articles