Clone a project that uses a Git submodule, open the folder where the shared library should be, and it is empty. Not an error, not a warning, just an empty directory and a build that fails a minute later. That empty folder sums up the trade between Git’s two ways of nesting one repository inside another: submodules keep the pieces separate and make everyone aware of them, subtrees copy everything in and make the nesting invisible. Both were tested here with Git 2.43, using a small library repository and two application repositories, one wired each way, and every command and output below is from those runs.
The problem both solve
Shared code across projects
A team has a library, a set of scripts or a design system used by several projects. Copying the files into each project by hand works until the first bug fix, when someone has to remember every copy. Package managers solve this for languages with good ones. For everything else, such as shell scripts, firmware, configuration or assets, Git offers two built-in answers.
The two answers in one line each
- A submodule stores a pointer to a specific commit of another repository. The other repository’s files are fetched separately.
- A subtree merges the other repository’s files, and optionally its history, into a folder of the main repository. Afterwards they are ordinary files.

Submodules: a pointer to a commit
Adding one
The Pro Git book’s chapter on submodules describes them as a way to keep a Git repository as a subdirectory of another Git repository. Adding the test library:
$ git submodule add ../lib vendor/lib
$ git commit -m "add lib submodule"
$ cat .gitmodules
[submodule "vendor/lib"]
path = vendor/lib
url = ../lib
$ git ls-tree HEAD vendor/lib
160000 commit 2f1e7f541a753554a8228a2263d49f00bc7315a7 vendor/lib
The main repository doesn’t store the library’s files at all. It stores a .gitmodules file with the library’s address, and one entry whose mode is 160000 and whose content is a commit ID. Pro Git calls that mode a special entry that records a commit as a directory entry instead of a file or folder.
The empty folder
A plain git clone of the application gave a vendor/lib folder with zero files in it. Pro Git explains: by default the clone gets the directories that contain submodules but none of the files within them, and the fix is git submodule init plus git submodule update, or cloning with --recurse-submodules to do both automatically. After git submodule update --init, the library’s file appeared, at version 1.
Pinned on purpose
The library then moved on to version 2 and later version 3. The application stayed exactly where it was. A fresh git clone --recurse-submodules of the app, made after the library reached version 3, still checked out version 1. The pointer names one commit, and nothing changes until someone moves it.
Moving it is a two-step job: update the submodule’s checkout, then commit the new pointer in the main project. In the test, pulling inside vendor/lib made the main repository report the submodule as modified:
$ git status --short
M vendor/lib
$ git diff --submodule
Submodule vendor/lib 2f1e7f5..9cf27dc:
> lib v2
The submodule surprises
Detached HEAD
After git submodule update, git status inside the submodule said HEAD detached at 2f1e7f5. Pro Git describes the same state and its risk: with no working branch, changes committed inside the submodule can easily be lost on the next git submodule update. Anyone editing the library from inside the application needs to check out a branch first.
Local paths stopped working in 2022
Adding the library from a path on the same disk failed in a fresh repository with the default settings:
fatal: transport 'file' not allowed
fatal: clone of '/tmp/p16/lib' into submodule path '/tmp/p16/app3/vendor/lib' failed
The Git 2.38.1 release announcement explains why. As part of fixing a security vulnerability, CVE-2022-39253, the default for protocol.file.allow changed to “user,” and GitHub’s write-up of the vulnerabilities notes that this makes file:// clones considered unsafe by default. The attack relied on a malicious repository cloned locally or packaged as a local submodule. Submodules hosted on GitHub or another server are unaffected. For a trusted local path, the per-command override is git -c protocol.file.allow=always submodule add ..., which is what the test used.
Removing one is a chore
Taking a submodule out of a project is more involved than deleting a folder. The .gitmodules entry, the gitlink in the main repository’s index and the submodule’s own copy inside the main project’s .git directory all need to go. Modern Git handles most of it with git rm vendor/lib, which removes the folder, the index entry and the .gitmodules section in one step, but the cached repository under .git/modules stays behind until it is deleted by hand. A subtree, by contrast, is removed like any other folder, because to Git it is just files.
Everyone has to know
Every clone needs the extra flag or the extra command. Every CI pipeline needs it too. Every switch between branches with different submodule pointers needs git submodule update, or the files on disk drift from what the main project expects. None of this is hard. All of it is easy to forget.

Subtree: the files move in
Adding one
The git-subtree documentation describes subtrees as including a subproject within a subdirectory of the main project, optionally with its full history, and points out that unlike submodules, they need no special constructions such as .gitmodules files or gitlinks. The same library, added to a second application:
$ git subtree add --prefix=vendor/lib ../lib main --squash
$ git log --oneline
baca369 Merge commit 'dab39ec...' as 'vendor/lib'
297a631 app
dab39ec Squashed 'vendor/lib/' content from commit 9cf27dc
Ordinary files
A plain git clone of that application, with no extra flags, had the library’s file in place immediately. The documentation’s point that users of the repository don’t need to know subtrees are involved held up: there was nothing for a newcomer to set up.
Updating it
When the library reached version 3, one command brought it in:
$ git subtree pull --prefix=vendor/lib ../lib main --squash -m "update lib"
The log gained one commit squashing the library’s changes and one merge. With --squash, the documentation says, the subtree’s history is collapsed into a single commit instead of merging every upstream commit, which keeps the main project’s log clean.
Sending changes back upstream
From a submodule
A submodule is a full repository, so changes made inside it are committed and pushed like any other repository, from inside the submodule folder, on a branch. Then the main project commits the new pointer. Two pushes, two repositories, nothing unusual.
From a subtree
A fix was made directly in vendor/lib inside the subtree application and committed there as an ordinary change. Getting it back to the library takes a split:
$ git subtree split --prefix=vendor/lib -b lib-export
$ git log --oneline lib-export
2128390 fix lib inside app2
44a60a1 lib v3
9cf27dc lib v2
2f1e7f5 lib v1
The documentation describes split as producing a new history containing only the commits that affected the prefix, with the subtree’s contents at the root, ready to export as its own repository. The result could be pushed to the library repository as a branch. git subtree push combines the split and the push into one step.
The trade-off
A subtree makes upstream contributions an extra step that people have to remember. A submodule makes downstream use an extra step that people have to remember. That is the whole choice in two sentences.

Choosing between them
Side by side
| Submodule | Subtree | |
|---|---|---|
| What the main repo stores | A commit pointer and a URL | The actual files |
| Fresh clone | Empty folder without --recurse-submodules |
Works as is |
| Updating the dependency | Update checkout, commit new pointer | git subtree pull |
| Contributing back | Commit and push inside the submodule | git subtree split or push |
| Who needs to know | Everyone who clones | Only whoever updates it |
| Repo size | Main repo stays small | Grows with the dependency’s files |
When submodules fit
Large dependencies that should not bloat the main repository, dependencies that are developed actively alongside the main project by people comfortable with Git, and cases where the exact pinned commit matters and should be reviewed as its own change.
When subtree fits
Small, slow-moving dependencies, projects with contributors or users who shouldn’t have to learn extra commands, and code that gets tweaked locally more often than it gets sent upstream. In both test applications the .git folders were close in size because the library was tiny; with a large dependency, the subtree approach carries all of it.
The empty folder after a plain clone is not a bug in submodules. It is the design showing through: the main project deliberately doesn’t contain the code, only a promise of where to find it. Subtree makes the opposite promise, everything is here, and leaves the bookkeeping to whoever updates it.
Sources
- Git Tools – Submodules, Pro Git (2nd edition), git-scm.com: https://git-scm.com/book/en/v2/Git-Tools-Submodules
- git-subtree documentation (contrib/subtree/git-subtree.adoc), Git source repository on GitHub: https://raw.githubusercontent.com/git/git/master/contrib/subtree/git-subtree.adoc
- [ANNOUNCE] Git v2.38.1 and others, Linux kernel mailing list archive: https://lore.kernel.org/lkml/xmqq4jw1uku5.fsf@gitster.g/
- Git security vulnerabilities announced (CVE-2022-39253), The GitHub Blog: https://github.blog/2022-10-18-git-security-vulnerabilities-announced/
