TExcelTAD
Home › Git › Git Submodules

Git Submodules

A submodule is a Git repository inside another Git repository. The outer project (the superproject) doesn't store the inner project's files; it stores a link: the inner repository's URL and the exact commit to use. Typical uses are shared libraries, themes, or vendored dependencies that have their own history.

website/                 <- superproject
├── .gitmodules          <- submodule URLs
├── index.html
└── themes/minimal/      <- submodule: pinned at commit 3fa2c1e of the theme repository

Note: For local paths like the ones in these examples, Git requires -c protocol.file.allow=always (a security default since Git 2.38). With https:// or SSH URLs on GitHub you don't need it.

Adding a submodule

bash · try it yourself
# a theme library with its own repository
git init -q --bare ~/github/theme.git
git clone -q ~/github/theme.git ~/theme-dev 2>/dev/null && cd ~/theme-dev
echo "body { font: 16px serif; }" > theme.css && git add . && git commit -q -m "Theme 1.0" && git push -q

# the website uses it as a submodule
mkdir ~/website && cd ~/website && git init -q
echo "<h1>Blog</h1>" > index.html && git add . && git commit -q -m "Add home page"
git -c protocol.file.allow=always submodule add ~/github/theme.git themes/minimal
git status -s
cat .gitmodules
git commit -q -m "Add minimal theme as a submodule"
git submodule status
Cloning into '/home/learner/website/themes/minimal'...
done.
A  .gitmodules
A  themes/minimal
[submodule "themes/minimal"]
	path = themes/minimal
	url = /home/learner/github/theme.git
 f07ca54fd79a3487b44cd57f2ba70bc5c29125ad themes/minimal (heads/main)

.gitmodules records where each submodule comes from. In the superproject's history, themes/minimal is a special entry (mode 160000, a gitlink) pointing to one commit of the theme:

bash · try it yourself
git init -q --bare ~/github/theme.git
git clone -q ~/github/theme.git ~/theme-dev 2>/dev/null && cd ~/theme-dev
echo "body { font: 16px serif; }" > theme.css && git add . && git commit -q -m "Theme 1.0" && git push -q
mkdir ~/website && cd ~/website && git init -q
echo "<h1>Blog</h1>" > index.html && git add . && git commit -q -m "Add home page"
git -c protocol.file.allow=always submodule add -q ~/github/theme.git themes/minimal
git commit -q -m "Add minimal theme as a submodule"

git ls-tree HEAD themes/
git -C themes/minimal log --oneline -1
160000 commit f07ca54fd79a3487b44cd57f2ba70bc5c29125ad	themes/minimal
f07ca54 Theme 1.0

Cloning a project with submodules

A plain git clone creates empty submodule folders. Either clone with --recurse-submodules, or run git submodule update --init afterwards:

bash · try it yourself
git init -q --bare ~/github/theme.git
git clone -q ~/github/theme.git ~/theme-dev 2>/dev/null && cd ~/theme-dev
echo "body { font: 16px serif; }" > theme.css && git add . && git commit -q -m "Theme 1.0" && git push -q
git init -q --bare ~/github/website.git
git clone -q ~/github/website.git ~/website 2>/dev/null && cd ~/website
echo "<h1>Blog</h1>" > index.html && git add . && git commit -q -m "Add home page"
git -c protocol.file.allow=always submodule add -q ~/github/theme.git themes/minimal
git commit -q -m "Add minimal theme" && git push -q

cd ~
git clone -q ~/github/website.git plain
echo "plain clone: $(ls plain/themes/minimal | wc -l) files in the submodule folder"
cd plain
git -c protocol.file.allow=always submodule update --init
ls themes/minimal
plain clone: 0 files in the submodule folder
Submodule 'themes/minimal' (/home/learner/github/theme.git) registered for path 'themes/minimal'
Cloning into '/home/learner/plain/themes/minimal'...
done.
Submodule path 'themes/minimal': checked out 'f07ca54fd79a3487b44cd57f2ba70bc5c29125ad'
theme.css

Updating a submodule

The superproject stays on the pinned commit until you deliberately move it. To take a new version of the theme, update the submodule and commit the new pointer:

bash · try it yourself
git init -q --bare ~/github/theme.git
git clone -q ~/github/theme.git ~/theme-dev 2>/dev/null && cd ~/theme-dev
echo "body { font: 16px serif; }" > theme.css && git add . && git commit -q -m "Theme 1.0" && git push -q
mkdir ~/website && cd ~/website && git init -q
echo "<h1>Blog</h1>" > index.html && git add . && git commit -q -m "Add home page"
git -c protocol.file.allow=always submodule add -q ~/github/theme.git themes/minimal
git commit -q -m "Add minimal theme"

# the theme authors release a new version
cd ~/theme-dev && echo "a { color: teal; }" >> theme.css && git commit -q -am "Theme 1.1: link colour" && git push -q

cd ~/website
git -c protocol.file.allow=always submodule update --remote
git status -s
git diff --submodule=log
git commit -q -am "Update minimal theme to 1.1"
git submodule status
From /home/learner/github/theme
   f07ca54..2a7ff59  main       -> origin/main
Submodule path 'themes/minimal': checked out '2a7ff59d09dde98f904a64f4d06140edbeb6a933'
 M themes/minimal
Submodule themes/minimal f07ca54..2a7ff59:
  > Theme 1.1: link colour
 2a7ff59d09dde98f904a64f4d06140edbeb6a933 themes/minimal (remotes/origin/HEAD)

--remote fetched the newest commit of the submodule's branch. git diff --submodule=log summarises which submodule commits the update brings in.

Commands at a glance

Command Does
git submodule add <url> <path> add a submodule
git clone --recurse-submodules <url> clone with submodules
git submodule update --init --recursive check out the pinned commits (after clone or pull)
git submodule update --remote move submodules to their branch's latest commit
git submodule status show pinned commits (- = not initialised, + = differs from pin)
git submodule foreach 'git pull' run a command in every submodule
git config --global submodule.recurse true make pull/switch update submodules automatically

Submodules or something else?

Submodules give you exact, reviewable pins, but every teammate must remember to update them. For language libraries, a package manager (npm, pip, Cargo, Maven) is usually simpler. Subtrees (git subtree) copy the other project's files into your repository instead of linking, which is easier for consumers but harder to send changes back.

Test yourself

What does the superproject store for a submodule?

  1. A full copy of its files
  2. Its URL (in .gitmodules) and the exact commit it should use
  3. Only its name
  4. A zip archive

After a plain git clone, the submodule folders are empty. What fixes that?

  1. git pull
  2. git submodule update --init
  3. git fetch --all
  4. git checkout .

The library you use as a submodule released a new version. What do you do?

  1. Nothing, it updates automatically
  2. git submodule update --remote, then commit the new pointer in the superproject
  3. Delete and re-add the submodule
  4. git push --recurse-submodules