Skip to main content

12 - Git Submodules and Subtree: Two Ways to Include a Repository

A submodule stores a pointer to a commit, a subtree copies the files into your history: what changes in the clone, the push and the commit count.

What do you think about this article?

12 - Git Submodules and Subtree: Two Ways to Include a Repository
Gitfedericocalo.dev

At a Glance

A submodule stores a pointer to a commit, a subtree copies the files into your history: what changes in the clone, the push and the commit count.

Including one repository inside another: two ways, two costs

Your project uses a library that you, or your team, maintain in a separate repository. You want it at hand in vendor/libreria, without copying it by hand every time it changes. Git offers two tools, and they are not interchangeable. The submodule does not carry the files: in your repository it stores only a pointer to a precise commit of the other one, and whoever clones must take one extra step to fill the folder. The subtree does the opposite: the files enter your repository, with a history of your own, and whoever clones finds them already there.

The price of the first is the effort of whoever collaborates, and the price of the second is a growing history, plus one more command when you want to give changes back. This is an advanced topic: it assumes you know the commit, the remote and the merge. You try both with the same library and look at what changes where daily work hurts: the clone, the update, the push, the number of commits. At the end you have a selection criterion built on what you saw, not on a preference.

Every block was run on 2026-11-19 with Git 2.53.0 (git --version), in a temporary folder outside the project, with local repositories only and no network access. Where a path was long I replaced it with [...]; your identifiers will differ. The outputs are the real ones, in English as Git prints them with the C locale; repository and commit names are in Italian because I ran the same script in both languages.

The submodule: a pointer, not a copy

The documentation (gitsubmodules) defines it as a repository embedded inside another: it has its own history, and the one that contains it is called the superproject. The superproject keeps track of it with two things.

The first is a special entry in the tree, the gitlink, which holds the identifier of the commit the folder must be at. The second is a section of the .gitmodules file, which holds, among other things, the address to download it from. The library's files never enter your history.

The first block uses a libreria repository with thirty commits and a v1.0 tag, and an app repository with one commit. The first git submodule add fails with "transport 'file' not allowed"; the second, with -c protocol.file.allow=always in front, succeeds. Then you look at three signs: git status shows two staged files, .gitmodules holds name, path and address, and git ls-tree shows for vendor/libreria an entry with mode 160000 and type commit, not a folder.

Adding a submodule, and what ends up in the repository
git submodule add /tmp/prova/libreria vendor/libreria
# Cloning into '[...]/app/vendor/libreria'...
# fatal: transport 'file' not allowed
# fatal: clone of '[...]/libreria' into submodule path '[...]/app/vendor/libreria' failed

git -c protocol.file.allow=always submodule add /tmp/prova/libreria vendor/libreria
# Cloning into '[...]/app/vendor/libreria'...
# done.

git status --short
# A  .gitmodules
# A  vendor/libreria

cat .gitmodules
# [submodule "vendor/libreria"]
# 	path = vendor/libreria
# 	url = [...]/libreria

git commit -m "app: aggiunge il sottomodulo"
git ls-tree HEAD vendor/libreria
# 160000 commit 9b0f92ef5d1ac4510ceb722b09c0cc60e70625a5	vendor/libreria
git submodule status
#  9b0f92ef5d1ac4510ceb722b09c0cc60e70625a5 vendor/libreria (v1.0)
git rev-list --count HEAD
# 2

The error on the file transport is not a fault, it is a security choice. According to the git config documentation, if you set nothing, the protocols considered safe (http, https, git, ssh) are always, while the others, file included, have the user policy. That policy applies to commands you run yourself, and not to those Git runs on its own, such as the recursive initialisation of submodules.

With https or ssh addresses you never meet it. You meet it if the submodule points to a local path: a test like this one, a mirror on disk, a CI that clones from a local copy. The value always unlocks the protocol, and it is better given with -c on the single command and not in the global configuration, so you do not leave the door open forever.

Whoever clones finds an empty folder

The real cost of the submodule is paid here. With a normal git clone of app, the vendor/libreria folder exists but is empty, and git submodule status shows the identifier preceded by a dash: the manual explains it as "submodule not initialized".

To fill it you need git submodule update --init, which registers the submodule, clones it and brings the folder to the commit named by the pointer. Inside, Git says "HEAD detached at 9b0f92e": you are not on a branch, you are on a commit, and that is intended.

Cloning a project with submodules, step by step
git clone app app-clone
ls -A vendor/libreria | wc -l
# 0
git submodule status
# -9b0f92ef5d1ac4510ceb722b09c0cc60e70625a5 vendor/libreria

git -c protocol.file.allow=always submodule update --init
# Submodule 'vendor/libreria' ([...]/libreria) registered for path 'vendor/libreria'
# Cloning into '[...]/app-clone/vendor/libreria'...
# done.
# Submodule path 'vendor/libreria': checked out '9b0f92ef5d1ac4510ceb722b09c0cc60e70625a5'

ls vendor/libreria
# VERSION
# note.txt
git -C vendor/libreria status | head -1
# HEAD detached at 9b0f92e

A well-known shortcut: git clone --recurse-submodules clones the project and fills the submodules in one go. With a submodule on a local path I found a trap: without precautions it fails, retries once and gives up. The reason is where you put -c. Written after clone it is not enough, and it must go before, as in the second command of the block.

With network addresses the problem does not arise, but it explains why a CI with local mirrors can fail in a way you never see on your laptop.

clone with --recurse-submodules: where the option goes
git clone --recurse-submodules app app-clone2 | head -4
# Cloning into 'app-clone2'...
# done.
# Submodule 'vendor/libreria' ([...]/libreria) registered for path 'vendor/libreria'
# Cloning into '[...]/app-clone2/vendor/libreria'...
# fatal: transport 'file' not allowed
# (it retries once and stops: "Failed to clone 'vendor/libreria' a second time, aborting")

git -c protocol.file.allow=always clone --recurse-submodules app app-clone2
# Cloning into 'app-clone2'...
# done.
# Submodule 'vendor/libreria' ([...]/libreria) registered for path 'vendor/libreria'
# Cloning into '[...]/app-clone2/vendor/libreria'...
# done.
# Submodule path 'vendor/libreria': checked out '9b0f92ef5d1ac4510ceb722b09c0cc60e70625a5'

Updating: two repositories, two steps

The library grows by one commit, number thirty-one. In the superproject the update is explicit: git submodule update --remote downloads from the submodule's remote and brings the folder to the latest commit of that branch. The manual says the remote is the one of the current branch (origin if you indicate nothing else) and that the branch is the remote's HEAD, unless you set submodule.<name>.branch in .gitmodules. I report that last setting from the manual and did not test it.

Right after, git status marks vendor/libreria as modified, because the pointer no longer matches the stored one, and git diff --submodule turns the difference into one readable line, the message of the new commit. Until you run git commit -am, the new pointer is a local change of yours.

After the commit, whoever updates app receives the new pointer but not the content: in the clone, after git pull, git submodule status shows the old identifier with a +, meaning "the checked-out commit does not match the one in the index". Only git submodule update aligns the folder.

Updating the submodule and propagating the pointer
# in app: the library has a new commit
git -c protocol.file.allow=always submodule update --remote
# From [...]/libreria
#    9b0f92e..dee4b03  main       -> origin/main
# Submodule path 'vendor/libreria': checked out 'dee4b0374f897d4c7a6853e75e34f6cc693dd55a'
git status --short
#  M vendor/libreria
git diff --submodule
# Submodule vendor/libreria 9b0f92e..dee4b03:
#   > libreria: modifica 31
git commit -q -am "app: aggiorna la libreria"

# in the clone: pull of the superproject
git -c protocol.file.allow=always pull -q
git submodule status
# +9b0f92ef5d1ac4510ceb722b09c0cc60e70625a5 vendor/libreria (v1.0)
git status --short
#  M vendor/libreria
git -c protocol.file.allow=always submodule update
# Submodule path 'vendor/libreria': checked out 'dee4b0374f897d4c7a6853e75e34f6cc693dd55a'
git submodule status
#  dee4b0374f897d4c7a6853e75e34f6cc693dd55a vendor/libreria (v1.0-1-gdee4b03)

In the clone, after the pull, git status flags vendor/libreria as modified when you touched nothing. It is the most surprising effect for whoever comes from single repositories: the new pointer arrived, the folder did not. Whoever does not know does git add and recommits the old pointer, undoing a colleague's update. The remedy is a habit, not a command: after every pull in a project with submodules, a git submodule update.

The push trap: a pointer nobody can follow

The submodule is a real repository, and you can work inside it. This is where trouble starts. I made a commit inside vendor/libreria without pushing it, and then a commit in app recording the new pointer.

If you pushed app, your colleagues would receive a pointer to a commit that exists only on your disk. Git has a check for this case: git push --recurse-submodules=check refuses to push and says which submodule has changes not found on any remote.

The check before the push
git push --recurse-submodules=check origin main
# The following submodule paths contain changes that can
# not be found on any remote:
#   vendor/libreria
#
# Please try
#
# 	git push --recurse-submodules=on-demand
#
# or cd to the path and use
#
# 	git push
#
# to push them to a remote.
#
# fatal: Aborting.
# fatal: the remote end hung up unexpectedly

The two ways out are the ones Git points to: --recurse-submodules=on-demand, which pushes the submodule first and then the superproject, or a cd into the folder and a manual push. The right order is always the library first, then whoever uses it. I did not run the on-demand option. The remote of app in the test is a local bare repository, and the second fatal line is part of the command's real output.

The subtree: the library becomes a folder of yours

The git subtree manual describes it as a subproject included in a subdirectory, even with its whole history, and distinguishes it from submodules: it needs no .gitmodules and no gitlink, and whoever uses your repository does not have to do anything special or understand how it works. It is a folder you commit to, branch and merge like the rest.

A note on the source: git subtree is a contrib command, and the page git-scm.com/docs/git-subtree answers 404, so here the source is the manual installed with Git, man git-subtree.

The next block repeats the operation in an app2 repository. With git subtree add --prefix=vendor/libreria --squash Git downloads the library and imports it into vendor/libreria with two commits: one holding the whole history "squashed" into a single commit, and a merge commit. The tree shows a normal folder (mode 040000, type tree), not a gitlink, and there is no .gitmodules. There are three commits, because the app's initial commit is added to those two. Whoever clones app2 finds the files immediately.

Adding a subtree and cloning the project
git subtree add --prefix=vendor/libreria --squash /tmp/prova/libreria main
# git fetch [...]/libreria main
# From [...]/libreria
#  * branch            main       -> FETCH_HEAD
# Added dir 'vendor/libreria'

git log --oneline --graph
# *   8f1331a Merge commit 'd02952fce4d9bafc57bc4b27e7f47b2f759f201e' as 'vendor/libreria'
# |\
# | * d02952f Squashed 'vendor/libreria/' content from commit dee4b03
# * 39c471e app: primo commit

git ls-tree HEAD vendor/libreria
# 040000 tree 9b0b6a12587538b5d179ce9a04f08093c1316914	vendor/libreria
ls -A
# .git
# README.md
# vendor
git rev-list --count HEAD
# 3

# in a clone of app2, with no other commands
git clone app2 app2-clone
ls vendor/libreria
# VERSION
# note.txt

The messages Added dir and the merge message are written by git subtree itself, and they are the memory of what you imported.

Updating and giving changes back

When the library grows (in the test, commit thirty-two), the update is a single command: git subtree pull --prefix=vendor/libreria --squash, with address and branch. Git downloads, creates a "squashed" commit covering the range dee4b03..c4c5778 and joins it with a merge.

The difference from the submodule is sharp: there, whoever collaborates must remember to update the folder; here it arrives with the normal git pull of app2.

The reverse path is less obvious, and it works. I edited note.txt inside vendor/libreria from app2, as in any folder, and committed. git subtree push extracted from the history only the commits that touch that folder, rewrote them with the content at the root, and pushed them to a new branch correzione of the original repository. Commit a947a52 sits on top of c4c5778, that is, on top of the library's real history. The manual guarantees that splitting the same history twice gives the same identifiers.

Updating from the original repository and sending corrections back
# the library has a new commit (32); in app2:
git subtree pull --prefix=vendor/libreria --squash /tmp/prova/libreria main
# From [...]/libreria
#  * branch            main       -> FETCH_HEAD
# Merge made by the 'ort' strategy.
#  vendor/libreria/note.txt | 1 +
#  1 file changed, 1 insertion(+)

git log --oneline --graph
# *   4097fba Merge commit '8286dbbef70b4d7a654699f630886c954e58f0a1'
# |\
# | * 8286dbb Squashed 'vendor/libreria/' changes from dee4b03..c4c5778
# * | 8f1331a Merge commit 'd02952fce4d9bafc57bc4b27e7f47b2f759f201e' as 'vendor/libreria'
# |\|
# | * d02952f Squashed 'vendor/libreria/' content from commit dee4b03
# * 39c471e app: primo commit

# change made by the app inside the folder, then:
git subtree push --prefix=vendor/libreria /tmp/prova/libreria-origin.git correzione
# To [...]/libreria-origin.git
#  * [new branch]      a947a52b776fb50bda517d7e43fddb84ae50432a -> correzione

git -C /tmp/prova/libreria-origin.git log --oneline -3 correzione
# a947a52 libreria: correzione fatta dall'app
# c4c5778 libreria: modifica 32
# dee4b03 libreria: modifica 31

For the test I pushed to a new branch of a bare repository, created with git clone --bare, which served only as a destination. I did not try git subtree split --rejoin, which the manual describes as the way to remind Git of the split point and speed up later runs, nor the split command on its own.

What the history costs

Here is the subtree cost you do not see until you count it. I added the same library, thirty-two commits at the time of the test, to two repositories: one with --squash, one without. The first has three commits in all. The second has thirty-four: thirty-two from the library, the app's initial commit and the Add 'vendor/libreria/' ... commit.

With the submodule the history of app counts two commits, and not one of the library's commits enters your repository.

How many commits enter your repository
# without --squash, library with 32 commits
git subtree add --prefix=vendor/libreria /tmp/prova/libreria main
git rev-list --count HEAD
# 34
git log --oneline | head -4
# a0582ff Add 'vendor/libreria/' from commit 'c4c5778d902f14beafe1601b7b0ae7487b004c3a'
# 39c471e app: primo commit
# c4c5778 libreria: modifica 32
# dee4b03 libreria: modifica 31

# summary of the three test repositories
# submodule:      2 commits
# subtree squash: 3 commits
# full subtree:  34 commits

Squash and full history do not mix. After adding the library with --squash and giving it a new commit (number thirty-three), I tried git subtree pull without the option. Git answered "refusing to merge unrelated histories". The manual says it about split --rejoin: if you do all your merges with --squash, use it there too. The practical rule is to choose once, --squash always or never, and write it in the project's README, because whoever comes after you will not see it anywhere in the code.

There is also a cost on release. git archive HEAD, which exports the files of a commit, lists for the subtree project all the library's files under vendor/libreria/. For the submodule project it lists .gitmodules and an empty vendor/libreria/ folder. If you build a source archive, the submodule is missing and the package is incomplete with no error to flag it. The subtree makes it complete, at the price of a heavier repository.

A release archive, with submodule and with subtree
# project with submodule
git archive HEAD | tar -t
# .gitmodules
# README.md
# vendor/
# vendor/libreria/

# project with subtree
git archive HEAD | tar -t | head -5
# README.md
# vendor/
# vendor/libreria/
# vendor/libreria/VERSION
# vendor/libreria/note.txt

How to choose

The facts do not say one tool is better: they say which cost you prefer to pay. My rule, which you may not share, starts from one question: who touches the library, and how often? The submodules manual lists as use cases the separation of histories, repository size, transfer size and access control. The four situations below are their practical translation.

  • Submodule, if the library is a project with a life of its own and several users, which you must pin to a precise version and update only when you decide.
  • Submodule, if the external repository is large or has many binary files you do not want in your history, or if access must be limited to whoever is entitled.
  • Subtree with --squash, if the library is small, you use it nearly as it is, and you want a simple clone to be enough: no extra step for colleagues and pipelines.
  • Subtree, if you often edit the library from inside the project and want to return the fixes with one command, without two working folders.

Every item has a cost that should not be hidden. The submodule shifts onto the team the discipline of submodule update and of push order, and a mistake produces pointers nobody can follow. The subtree shifts onto the repository the weight of the history and onto the team the --squash rule, and a mistake produces a merge refusal. If the library is yours and the team is small, a third way is often the most sensible: publish it with a version number and declare it among the dependencies. How to give that number is in Git Tag: A Name for Every Release.

What you can do now

You can tell the two tools apart by what ends up in the tree: a 160000 entry with a pointer for the submodule, a normal folder for the subtree. You can clone a project with submodules without leaving the folder empty, and you know why -c protocol.file.allow=always goes in front of clone.

You can read the - and + signs of git submodule status and prevent a pointer to a never-published commit with --recurse-submodules=check. With the subtree you can add, update and give changes back, and you can quantify the cost of history, from three to thirty-four commits. What I did not try stays out: on-demand, split --rejoin, removing a submodule and very large repositories.

Frequently asked questions

What is the difference between a submodule and a subtree?

A submodule stores in your repository only a pointer to a commit of another repository, and whoever clones must download it with an extra step. A subtree copies the files into your history: you clone and use it right away, but the history grows.

Why is the submodule folder empty after a clone?

Because the clone brings only the pointer. The folder is filled with git submodule update --init, or by cloning from the start with --recurse-submodules.

Why does Git say "transport 'file' not allowed"?

Because for the file protocol the default policy is user, which does not apply to operations run automatically, such as cloning a submodule. With https or ssh addresses it does not happen; with a local path you unlock it with git -c protocol.file.allow=always, written before the command.

Should I use --squash with git subtree?

It depends on how much of the library's history you want in your repository. In the test, 32 library commits led to 34 commits without --squash and to 3 with it. The important thing is not to mix the two: a pull without the option after an add with the option was refused.

Sources

  • gitsubmodules - Mounting one repository inside another - definition of submodule, gitlink and .gitmodules, the use cases; read locally with man gitsubmodules and opened on the web (HTTP 200) on 2026-11-19
  • git-submodule - official Git documentation - update --init, update --remote, the - and + signs of status; read locally with man git-submodule and opened on the web (HTTP 200) on 2026-11-19
  • git-config - official Git documentation - protocol.allow and the user policy; read locally with man git-config and opened on the web (HTTP 200) on 2026-11-19
  • The documentation of git subtree lives in the Git repository, under contrib/subtree, and in the installed manual (man git-subtree; source /usr/share/doc/git/contrib/subtree/git-subtree.adoc), read on 2026-11-19. The page git-scm.com/docs/git-subtree answers 404 and is not cited as a source.
  • Examples run with Git 2.53.0 (git --version) in local test repositories, on 2026-11-19, in Italian and with LC_ALL=C.

Try These Tools

Free developer tools related to this topic.

Commenti

Caricamento commenti...

Sign in to comment

New articles and tools on Telegram too

The Telegram channel flags the site's new articles and tools.

Did you like this article?

Explore other content on the blog or discover my projects