Git Submodules

Updated · 11 min read
hardGit 2.53gitsubmodulesdependenciesmonorepointerview

A submodule is a Git repository placed inside a folder of another Git repository. The outer repository, called the superproject, doesn't store the submodule's files. It stores the submodule's URL and the exact commit of it to use. It is used to include code that has its own history and its own team, such as a shared library, a theme or a set of configuration files, and to pin it to a known version.

A recipe book is a good picture. Instead of copying a sauce recipe into every recipe that needs it, the book says "use the sauce from page 12 of the 2026 edition of the sauce book". The sauce book is kept and updated separately, and each recipe says exactly which edition it was tested with.

Syntax

Terminal
git submodule add <url> <path>
git clone --recurse-submodules <url>
git submodule update --init --recursive
git submodule update --remote
CommandDescription
git submodule add <url> <path>It is used to clone a repository into path and record it as a submodule.
git submodule statusIt is used to show each submodule's recorded commit and path.
git clone --recurse-submodules <url>It is used to clone a project together with all its submodules.
git submodule update --init --recursiveIt is used to download and check out the recorded commit of every submodule (and of submodules inside them).
git submodule update --remoteIt is used to move each submodule to the newest commit of its branch on the remote.
git submodule foreach <command>It is used to run a command in every submodule.
git submodule deinit <path>It is used to empty a submodule's folder and stop updating it.

The Starting Point

The school's fee rules are kept in their own repository, fee-rules, because several projects use them. The Fee Tracker wants to include them. As in "Git Remote", bare repositories in folders stand in for the server: fee-rules.git and fee-tracker.git.

Terminal
git init --quiet fee-rules
cd fee-rules
echo "Late fee after day: 10" > rules.txt
git add rules.txt
git commit --quiet --author="Srihitha Anamaneni <srihitha@example.com>" -m "Add the late fee rule"
cd ..
git clone --quiet --bare fee-rules fee-rules.git
git init --quiet fee-tracker
cd fee-tracker
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git remote add origin ../fee-tracker.git
git init --quiet --bare ../fee-tracker.git
git push --quiet -u origin main
Output
 

Since Git 2.38.1, Git refuses to clone a submodule from a local folder path, to protect against a security problem with untrusted repositories. This page uses folders as servers, so it allows them once. Don't change this setting for real work, where submodule URLs are https:// or git@ addresses:

Terminal
git config --global protocol.file.allow always
Output
 

Example 1: Adding a Submodule

Terminal
git submodule add ../fee-rules.git rules
git status --short
cat .gitmodules
Output
Cloning into '/home/navika/projects/fee-tracker/rules'...
done.
A  .gitmodules
A  rules
[submodule "rules"]
	path = rules
	url = ../fee-rules.git

Explanation: Git cloned fee-rules.git into the folder rules and staged two things: a new file .gitmodules, which records the submodule's path and URL, and the folder rules itself. The URL ../fee-rules.git is relative: Git resolves it against the superproject's own origin URL. For a project at https://github.com/school/fee-tracker.git, it would mean https://github.com/school/fee-rules.git. Relative URLs are a good habit when both repositories live on the same server, because they keep working for clones made over HTTPS and SSH alike.

What Git staged for rules is not its files, but one commit ID:

Terminal
git ls-files --stage rules
git -C rules log --oneline
Output
160000 494241e2a5fb602196aace532e38808879aa9e6b 0	rules
494241e (HEAD -> main, origin/main, origin/HEAD) Add the late fee rule

Explanation: the mode 160000 marks a gitlink: an entry that points to a commit in another repository. The superproject stores only that ID. Inside rules is a complete repository with its own history.

Terminal
git commit --quiet -m "Add the fee rules as a submodule"
git push --quiet
git submodule status
Output
 494241e2a5fb602196aace532e38808879aa9e6b rules (heads/main)

git submodule status shows the pinned commit ID, the path, and the nearest branch name in brackets.

The superproject stores a URL and one pinned commit

Example 2: Cloning a Project with Submodules

Advitha clones the Fee Tracker the ordinary way:

Terminal
cd ..
git clone --quiet fee-tracker.git advitha
cd advitha
ls rules | wc -l | tr -d " "
git submodule status
Output
0
-494241e2a5fb602196aace532e38808879aa9e6b rules

Explanation: the rules folder exists but is empty, and git submodule status marks it with -: not initialized. A plain clone does not download submodules. She fetches them:

Terminal
git submodule update --init --recursive
cat rules/rules.txt
Output
Submodule 'rules' (/home/navika/projects/fee-rules.git) registered for path 'rules'
Cloning into '/home/navika/projects/advitha/rules'...
done.
Submodule path 'rules': checked out '494241e2a5fb602196aace532e38808879aa9e6b'
Late fee after day: 10

--init copies the URL from .gitmodules into her own config ("registered for path"), and update clones the submodule and checks out the pinned commit. --recursive does the same for submodules inside submodules. git clone --recurse-submodules <url> does all of this during the clone, and is the command to remember.

Example 3: Updating a Submodule to a Newer Version

Srihitha improves the fee rules in the library's own repository:

Terminal
cd ../fee-rules
git remote add origin ../fee-rules.git
echo "Sibling discount: 10%" >> rules.txt
git commit --quiet -am "Add the sibling discount rule" --author="Srihitha Anamaneni <srihitha@example.com>"
git push --quiet origin main
Output
 

The Fee Tracker still uses the old commit. Navika moves it to the newest one:

Terminal
cd ../fee-tracker
git submodule update --quiet --remote rules
git status --short
git diff --submodule=log
Output
From /home/navika/projects/fee-tracker/../fee-rules
   494241e..b633e3f  main       -> origin/main
 M rules
Submodule rules 494241e..b633e3f:
  > Add the sibling discount rule

Explanation: --remote fetched the submodule's branch (the From lines; --quiet hides only the update's own messages) and checked out its newest commit. The superproject sees the folder as modified: its pinned commit changed. git diff --submodule=log lists the library commits that the change brings in. Like any change, it must be committed and pushed:

Terminal
git commit --quiet -am "Update the fee rules to the sibling discount"
git push --quiet
Output
 

Instead of --remote, you can also go into the folder and use normal commands, such as git -C rules switch main and git -C rules pull, then commit the change in the superproject.

Example 4: The Forgotten Update

Advitha pulls Navika's work:

Terminal
cd ../advitha
git pull --quiet
git status --short
cat rules/rules.txt
Output
 M rules
Late fee after day: 10

Explanation: git pull moved the superproject to the new pinned commit, but didn't move the submodule's files. rules shows as modified, because it is still on the old commit, and rules.txt has no discount rule. If she committed now with -a, she would undo Navika's update by pinning the old commit again. This is the most common submodule mistake. The fix:

Terminal
git submodule update
cat rules/rules.txt
git status --short
Output
Submodule path 'rules': checked out 'b633e3f3753220eb32d672b7c166162e244990f5'
Late fee after day: 10
Sibling discount: 10%

To make pull, switch and other commands update submodules automatically, set:

Terminal
git config --global submodule.recurse true

Detached HEAD Inside a Submodule

git submodule update checks out a commit, not a branch:

Terminal
git -C rules status | head -1
Output
HEAD detached at b633e3f

That is correct for using the library. But if you want to change the library from inside the submodule folder, first switch to a branch (git -C rules switch main), or your commits will be on a detached HEAD and easy to lose (see "HEAD and Detached HEAD").

Removing a Submodule

Terminal
git rm rules
git commit -m "Remove the fee rules submodule"

git rm removes the folder and its entry in .gitmodules. The cloned repository stays in .git/modules/rules until you delete that folder too.

Common Mistakes

Cloning without --recurse-submodules. The submodule folders are empty. Run git submodule update --init --recursive.

Pulling without updating submodules. The submodule stays on the old commit and shows as modified. Run git submodule update, or set submodule.recurse true.

Committing an old submodule commit by accident. git commit -a after a pull without git submodule update pins the old version again. Check git status before committing.

Pushing the superproject before the submodule. Teammates can't fetch the pinned commit.

Making commits on a detached HEAD inside a submodule. Switch to a branch first.

Under the Hood

A submodule has three parts. The gitlink is an entry in the superproject's tree with mode 160000 and a commit ID instead of a blob ID; this is the pinned version, and it changes only when you commit a new one. The .gitmodules file, committed with the project, maps each path to a URL (and optionally a branch) so that clones know where to fetch from. git submodule init copies those URLs into .git/config, where you can override them locally. The submodule's own repository lives in .git/modules/<path>/, and the submodule folder has a .git file pointing there, which is why git rm can remove the folder without losing the repository. Because the superproject stores only a commit ID, Git never mixes the library's history into the project's history.

Interview Corner

What is a Git submodule?

A Git repository inside a folder of another repository. The outer repository records the submodule's URL in .gitmodules and pins an exact commit of it, without storing its files.

How do you clone a project with submodules?

git clone --recurse-submodules <url>. If you already cloned without it, run git submodule update --init --recursive.

How do you update a submodule to its latest version?

git submodule update --remote <path> (or pull inside the submodule folder), then commit the new pinned commit in the superproject and push.

Why is a submodule on a detached HEAD?

git submodule update checks out the exact pinned commit, not a branch. To make changes inside it, switch to a branch first.

Practice

Challenge: a third clone. Clone the Fee Tracker as srihitha in one step, with its submodule, and print the submodule's status and the last line of rules/rules.txt.

What does Git print? Before you open the solution, decide whether the status line starts with -, a space or +.

Show solution
Terminal
cd ..
git clone --quiet --recurse-submodules fee-tracker.git srihitha
git -C srihitha submodule status
tail -1 srihitha/rules/rules.txt
Output
 b633e3f3753220eb32d672b7c166162e244990f5 rules (heads/main)
Sibling discount: 10%

The line starts with a space: the submodule is initialized and on the pinned commit (a + would mean it is on a different commit, and - not initialized). The rules include the sibling discount, because the clone checked out the newest pinned commit.

Checkpoint. Add a small public repository as a submodule of a practice project, clone the project again with --recurse-submodules, update the submodule with --remote, and commit the new version.

Quick check: Git submodules

4 questions
1What does the superproject store for a submodule?
2You cloned a project and its submodule folder is empty. What fixes it?
3After git pull, git status shows the submodule as modified, and you changed nothing. Why?
4Which command moves a submodule to the newest commit of its branch?

Key Takeaways

Quick reference

CommandIt is used to
git submodule add <url> <path>Add a submodule
git clone --recurse-submodules <url>Clone with submodules
git submodule update --init --recursiveFetch and check out pinned commits
git submodule update --remoteMove to the newest commit of the branch
git submodule statusShow pinned commits
git config submodule.recurse trueUpdate submodules automatically

Was this tutorial helpful?

Lesson 89 of 106