Submodules: Repositories Inside Repositories
Include another repository at a fixed commit with git submodule: adding one, what .gitmodules and the gitlink record, cloning with --recurse-submodules, updating to new versions, working inside a submodule without losing commits, pushing safely, changing URLs, and removing a submodule cleanly.
kvi5hnuCode Sprout team member7 min readOn this page
Sometimes a project needs another project's code: a shared library, a theme, a set of configuration files that several repositories use. A submodule places another Git repository inside yours, in a folder, and records exactly which commit of it your project uses. Each project keeps its own history, and updating the included code is an explicit, reviewable change. Submodules are powerful but have sharp edges, and this article covers both: the commands, and the mistakes that commonly lose work.
The examples use local folders as "remote" repositories. Since Git 2.38.1, Git refuses submodules from local paths by default, for security, so the examples start by allowing them. You won't need that setting with real URLs.
Adding a submodule
git config --global protocol.file.allow always # sandbox only: allow local-path submodules
git init -q --bare maps-lib.git
git clone -q maps-lib.git maps-lib 2>/dev/null; cd maps-lib
echo "export const version = 1;" > map.js; git add .; git commit -q -m "Map library v1"; git push -q origin main
cd ..
git init -q --bare trail-guide.git
git clone -q trail-guide.git trail-guide 2>/dev/null; cd trail-guide
git commit -q --allow-empty -m "Start project"
git submodule add -q ../maps-lib.git libs/maps
echo "=== .gitmodules"
cat .gitmodules
git status --short
git commit -q -m "Add maps library as a submodule"
echo "=== what the commit stores for libs/maps"
git ls-tree HEAD libs/
git push -q origin main
=== .gitmodules
[submodule "libs/maps"]
path = libs/maps
url = ../maps-lib.git
A .gitmodules
A libs/maps
=== what the commit stores for libs/maps
160000 commit f26c4691d58de642dc28d5835120838c6ab59dea libs/maps
git submodule add <url> <path> clones the other repository into the folder and stages two things:
.gitmodules, a committed file mapping each submodule's path to its URL. A relative URL like../maps-lib.gitis resolved against your own remote's URL, which keeps working if both projects move to another host together.- a gitlink for
libs/maps: a tree entry with mode160000holding a commit ID, not files. Your repository stores "this folder is maps-lib at commit<id>", and nothing of maps-lib's content.
The submodule's own repository data lives in .git/modules/libs/maps, and its folder contains a .git file pointing there, like a linked worktree.
Cloning a project with submodules
A plain clone creates the submodule folders but leaves them empty:
git config --global protocol.file.allow always
git init -q --bare maps-lib.git; git clone -q maps-lib.git ml 2>/dev/null
(cd ml && echo "v1" > map.js && git add . && git commit -q -m "Map library v1" && git push -q origin main)
git init -q --bare trail-guide.git; git clone -q trail-guide.git tg 2>/dev/null
(cd tg && git commit -q --allow-empty -m "Start project" && git submodule add -q ../maps-lib.git libs/maps && git commit -q -m "Add maps" && git push -q origin main)
git clone -q trail-guide.git ana
cd ana
echo "files in libs/maps after a plain clone: $(ls -A libs/maps | wc -l)"
git submodule status # - means not initialized
git submodule update --init --recursive -q
git submodule status
cd libs/maps; git status | head -1
files in libs/maps after a plain clone: 0
-5eb03a36461f4bd1c4d858a28cf7c52532211cc3 libs/maps
5eb03a36461f4bd1c4d858a28cf7c52532211cc3 libs/maps (heads/main)
HEAD detached at 5eb03a3
git clone --recurse-submodules <url>does it in one step. For an existing clone,git submodule update --init --recursiveclones and checks out every submodule, including nested ones.git submodule statusprefixes-for uninitialized submodules,+when the checked-out commit differs from the one recorded, andUfor conflicts.- The submodule is checked out at the recorded commit, in detached HEAD. That's by design, since your project pins a commit, not a branch.
Set git config --global submodule.recurse true to make pull, switch, checkout and reset update submodules automatically, which avoids the most common confusion: pulling a commit that changed a submodule, but still having the old version checked out.
Updating a submodule to a newer version
Your project uses a fixed commit of the library until you deliberately move it:
git submodule update --remote libs/maps # fetch, and check out the tip of its branch
git diff --submodule=log # what changed in it
git add libs/maps
git commit -m "Update maps library to v2"
--remote uses the submodule's remote default branch, or the branch set with git submodule set-branch --branch stable libs/maps, which records branch = stable in .gitmodules. Alternatively, go into the folder and check out any commit or tag:
cd libs/maps
git fetch
git switch --detach v2.1.0
cd ../..
git add libs/maps && git commit -m "Use maps library v2.1.0"
Either way, the change in your project is just the gitlink moving from one commit ID to another, which shows up in git status as M libs/maps. Settings that make it readable: git config --global diff.submodule log (diffs list the submodule's commits) and status.submoduleSummary true.
Working inside a submodule
You can fix the library from within your project, but two traps lose commits:
- Detached HEAD. After
update, the submodule isn't on a branch. Commits made there belong to no branch, and the nextgit submodule updatemoves away from them. Switch to a branch first:cd libs/maps && git switch main. - Pushing in the wrong order. If you commit the new gitlink in your project and push it, but don't push the submodule's commit, everyone else's
submodule updatefails, because the commit your project points to doesn't exist on the library's server.
cd libs/maps
git switch main # 1. work on a branch
# edit, commit
git push # 2. push the library first
cd ../..
git add libs/maps
git commit -m "Use maps library with the zoom fix"
git push --recurse-submodules=check # refuses if a submodule commit isn't pushed
Set push.recurseSubmodules=check to make that check automatic, or on-demand to push submodules first automatically.
Running commands in every submodule
git submodule foreach '<command>' runs a shell command in each submodule, with variables such as $name and $sm_path:
git submodule foreach 'git log --oneline -1'
git submodule foreach --recursive 'git switch main && git pull'
Changing a submodule's URL
When the library moves, update .gitmodules, then copy the change into each clone's configuration:
git submodule set-url libs/maps https://git.example.com/acme/maps-lib.git
git commit -am "Move maps library URL"
# teammates, after pulling:
git submodule sync --recursive
git submodule update --init --recursive
sync is needed because each clone's .git/config keeps its own copy of the URL, set when the submodule was initialized.
Removing a submodule
git rm libs/maps # removes the folder, the gitlink and the .gitmodules entry
git commit -m "Remove maps submodule"
rm -rf .git/modules/libs/maps # optional: delete its stored repository too
git submodule deinit libs/maps is the softer option: it empties the folder and removes its configuration in your clone, but keeps the submodule in the project, which is useful when you don't need a large submodule locally.
Submodules or something else?
Submodules suit including another independently developed repository at a pinned version, especially when you need its history or contribute back to it. Their cost is extra commands for everyone, and confusion when people forget them. Alternatives:
- A package manager (npm, pip, Maven, Cargo, Go modules) is usually better for libraries that publish versions.
git subtreecopies the other project's files into your repository, so cloning needs nothing special (article 42).- A monorepo keeps related projects in one repository from the start.
What's current
submodule.recurse=truemakes most commands handle submodules automatically and is widely recommended.- Since Git 2.38.1,
protocol.file.allowdefaults touser, blocking submodules from local paths unless you allow them, to protect against malicious repositories. git clone --recurse-submodulesand CI systems' checkout actions (such assubmodules: recursivein GitHub Actions) handle submodules in builds.
Common questions
I cloned a project and the submodule folder is empty. Why?
Submodules aren't cloned by default. Run git submodule update --init --recursive, or clone with --recurse-submodules.
git status shows modified: libs/maps (new commits) after a pull. What do I do?
Your project's commit now points to a different submodule commit than the one checked out. Run git submodule update to check it out, or set submodule.recurse=true so pull does it.
I committed inside a submodule, and after git submodule update my commit is gone. Can I get it back?
Yes, from the submodule's reflog: cd libs/maps && git reflog, then git branch rescue <id> (article 26). Next time, switch to a branch before committing in a submodule.
Why does git submodule add fail with "transport 'file' not allowed"?
The URL is a local path, which Git blocks by default since 2.38.1. Use the real remote URL, or, if you trust the repository, allow it for one command: git -c protocol.file.allow=always submodule add ....
Summary
git submodule add <url> <path>records another repository at a specific commit:.gitmodulesholds URLs, and a gitlink (mode160000) holds the commit ID.- Clone with
--recurse-submodules, or rungit submodule update --init --recursive. Setsubmodule.recurse=true. - Submodules are checked out in detached HEAD. Switch to a branch before committing in one.
- Update with
git submodule update --remoteor by checking out a commit inside, then commit the new gitlink. - Push the submodule first.
push.recurseSubmodules=checkenforces that. set-urlwithsyncchanges URLs, andgit rm <path>removes a submodule.
Practice questions
A teammate pushed a commit that updates the libs/maps submodule. After git pull, your build still uses the old library. Why, and what's the fix?
Answer
git pull updated the gitlink in your project, but didn't check out the new submodule commit, because submodules aren't updated automatically by default. Run git submodule update --init --recursive. To avoid it in future, git config --global submodule.recurse true.
You fixed a bug inside libs/maps, committed in your project and pushed, and now CI fails with "fatal: remote error: upload-pack: not our ref". What went wrong?
Answer
The project points to a submodule commit that exists only on your machine: the submodule's commit was never pushed to the library's own remote. Go into libs/maps, make sure the commit is on a branch, and push it. Then CI can fetch it. git push --recurse-submodules=check, or push.recurseSubmodules=check, prevents this.
How do you remove the libs/old-theme submodule from a project?
Answer
git rm libs/old-theme, which removes the folder, its gitlink and its .gitmodules entry, then commit. Optionally delete the stored repository with rm -rf .git/modules/libs/old-theme in each clone.

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.