Log inSign up

Nest repositories with Git submodules

Add another repository as a subfolder with git submodule add URL path and commit the new .gitmodules file together with the pinned commit. Clone projects that use submodules with git clone --recurse-submodules, or run git submodule update --init --recursive in an existing clone; move a submodule forward with git submodule update --remote and commit the new pointer.

Topic: Git on a server · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What problem do submodules solve?

A submodule lets one repository contain another as a folder while each keeps its own history. The parent repository does not store the files of the child; it stores the URL in .gitmodules and the exact commit the child should be at. Everyone who checks out the parent gets that same commit.

Typical uses are a shared library used by several services, a theme kept in its own repository, or configuration that several projects reuse. The pinned commit is the key feature and also the main source of confusion: a submodule does not follow a branch by itself, it stays where the parent says until someone updates and commits the pointer. If a simple copy of the files would do, or a package manager can deliver the dependency, those options are easier to live with. Make sure Git is set up as in install and configure Git.

How do you add a submodule?

Run git submodule add with the repository address and the folder where it should live, then commit.

cd /srv/app
git submodule add git@git.example.net:team/shared-lib.git lib/shared
git status
cat .gitmodules
git commit -m "Add shared-lib as a submodule"
git push

The status shows two new entries: .gitmodules and lib/shared, which Git records as a special commit pointer rather than a normal folder. To follow a particular branch when updating later, add -b main to the add command, or set it afterwards with git config -f .gitmodules submodule.lib/shared.branch main.

How do you clone a project that has submodules?

Add --recurse-submodules to the clone so Git fetches each submodule and checks out the pinned commit in one go.

git clone --recurse-submodules git@git.example.net:team/app.git

# already cloned without it? Fill the empty folders:
git submodule update --init --recursive
git submodule status

Without either step the submodule folders stay empty, which is the classic cause of "file not found" errors right after a deploy. git submodule status prints each pinned commit; a leading - means not initialised, + means the checked-out commit differs from the recorded one. For a server that only needs files, add --shallow-submodules to reduce download size.

How do you keep submodules in step after a pull?

A plain git pull updates the parent pointer but does not move the submodule checkout, so run an update after it, or make Git do that for you.

git pull
git submodule update --init --recursive

# or once per machine:
git config --global submodule.recurse true

With submodule.recurse enabled, pull, switch and checkout also update submodules. Deploy scripts should still call git submodule update --init --recursive explicitly, because they may run as a user without that setting, as in the deploy hooks guide.

How do you move a submodule to a newer commit?

Update the submodule, check the result, and commit the new pointer in the parent repository.

git submodule update --remote lib/shared     # follow the configured branch
# or by hand:
cd lib/shared
git fetch
git switch --detach v2.3.0
cd ../..

git diff --submodule                         # shows old..new commit range
git add lib/shared
git commit -m "Bump shared-lib to v2.3.0"
git push

Forgetting the last commit in the parent is the most common slip: the submodule moved on your machine, but nobody else sees it. git config --global diff.submodule log makes git diff list the submodule commits by subject, which helps when reviewing a bump.

How do you change code inside a submodule?

Treat the submodule folder as its own repository: switch to a branch, commit, push it, and then commit the new pointer in the parent.

cd lib/shared
git switch main
# edit, then:
git commit -am "Handle empty config"
git push
cd ../..
git add lib/shared
git commit -m "Use shared-lib with empty config fix"
git push

After an update the submodule sits on a detached HEAD, so commit only after switching to a branch, otherwise your commit is easy to lose. Push the submodule before the parent; if you push only the parent, others get a pointer to a commit their clone cannot fetch. git push --recurse-submodules=check refuses the parent push in that case, and =on-demand pushes the submodules first automatically.

How do you change a submodule URL or run a command in all of them?

Edit the URL in .gitmodules, then sync it into the local configuration of every clone.

git config -f .gitmodules submodule.lib/shared.url https://git.example.net/team/shared-lib.git
git submodule sync --recursive
git submodule update --init --recursive
git commit -am "Fetch shared-lib over HTTPS"

git submodule foreach --recursive "git status -s"
git submodule foreach "git fetch --tags"

git submodule foreach runs a shell command inside each initialised submodule, with variables like $name and $sm_path available, which is handy for status checks across many of them. Read-only deploy servers can use an HTTPS URL or a deploy key per submodule; see the SSH keys tutorial for key handling.

How do you remove a submodule cleanly?

Deinitialise it, remove it with git rm, delete its cached data and commit.

git submodule deinit -f lib/shared
git rm -f lib/shared
rm -rf .git/modules/lib/shared
git commit -m "Remove shared-lib submodule"

git rm also deletes its section from .gitmodules. The .git/modules folder holds the submodule repository data; leaving it does no harm, but removing it avoids confusion if you later add a different repository at the same path.

Which submodule errors come up most?

  • Empty folder after clone: run git submodule update --init --recursive.
  • "reference is not a tree" or "not our ref": the parent points at a submodule commit that was never pushed. Push it from the machine that made it.
  • Permission denied while updating: the server key can read the parent but not the submodule repository. Grant access or switch that submodule to a URL the server can read.
  • Submodule always shows as modified: build output inside it changes files. Add ignore = dirty for that submodule in .gitmodules, or fix the build to write elsewhere.

Questions about Git submodules

Submodule or subtree: which should I use?

Submodules keep a pointer and separate history; subtrees copy the files into the parent repository. Subtrees are simpler for people who only clone, submodules are cleaner when the child is developed actively on its own.

Why is my submodule in detached HEAD state?

Git checks out the exact commit recorded by the parent, not a branch. That is intended. Switch to a branch inside the submodule before you make commits there.

Can a submodule use a relative URL?

Yes. A URL like ../shared-lib.git resolves against the parent remote, which keeps working when the whole group of repositories moves to another host.

Do submodules work with bare repositories on my own server?

Yes. Any address Git can clone from works, including ssh://deploy@198.51.100.24/srv/git/shared-lib.git on a VPS you run yourself.

How do I see which commit each submodule is pinned to?

Run git submodule status --recursive or git ls-tree HEAD lib/shared, which prints the commit recorded in the parent.