Submodules and subtrees
Include one repository inside another at a pinned commit with submodules, or copy it in with subtree. See how each is stored, what a clone gets, and the traps.
Submodules and subtrees
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A submodule is a pointer: the parent repository stores a gitlink (mode `160000`) naming a specific commit of another repository, plus the URL in `.gitmodules`.
- A plain `git clone` creates empty submodule folders, and you need `git clone --recurse-submodules` or `git submodule update --init` to fill them.
- After the parent pulls a new submodule pointer, run `git submodule update`, or `git status` shows the submodule as modified because it is still at the old commit.
- `git subtree add --prefix=<dir> <repo> <branch> --squash` copies another repository's content into a folder as ordinary files, so clones need no extra steps.
- Choose a submodule when you need an exact, separate, independently versioned dependency, and subtree when you want simple clones and are happy with the code living in your history.
Sharing code between repositories
jot has grown a formatting helper, fmt.sh, that two other projects also want. Copying the file works until someone fixes a bug in one copy. You need to include a repository inside another repository without losing track of which version you’re using. Git has two answers with different trade-offs.
I ran both on Git 2.54, using local bare repositories as the “servers”. Hashes will differ for you.
Submodules: a pinned pointer
$ git submodule add ../fmt.git lib/fmt
Cloning into '.../jot/lib/fmt'...
fatal: transport 'file' not allowed
First surprise: recent Git blocks cloning from a local path in a submodule, as a security measure. With a real https:// or ssh:// URL you won’t see this. For local practice, allow it for that one command:
$ git -c protocol.file.allow=always submodule add ../fmt.git lib/fmt
$ git status -sb
## main
A .gitmodules
A lib/fmt
$ cat .gitmodules
[submodule "lib/fmt"]
path = lib/fmt
url = ../fmt.git
Git added two things to the next commit. .gitmodules records the path and URL. And lib/fmt is staged, but not as files:
$ git commit -qm "Add fmt as a submodule"
$ git ls-tree HEAD lib/
160000 commit 003e5fda80685ddaaefc1cb06102420d1c6d2b00 lib/fmt
Mode 160000 and type commit: a gitlink. The parent’s tree names a commit in a different repository, and nothing else. In terms of objects, the submodule is a tree entry that points to a commit you can’t find in this object database. That is how it’s pinned: everyone who checks out this commit of jot gets that exact commit of fmt.
What a teammate gets
$ git clone jot copy
$ ls copy/lib/fmt | wc -l
0
$ git -C copy submodule status
-003e5fda80685ddaaefc1cb06102420d1c6d2b00 lib/fmt
The folder is empty, and the leading - means “not initialised”. Fill it:
$ git -C copy -c protocol.file.allow=always submodule update --init
Submodule path 'lib/fmt': checked out '003e5fda80685ddaaefc1cb06102420d1c6d2b00'
Or do it at clone time with git clone --recurse-submodules <url>. The relative URL ../fmt.git resolved against the parent’s own remote, so it works after a move to another host. Forgetting this step is the number one submodule complaint.
Updating, and the trap
The fmt project publishes a new commit. In the parent:
$ git -c protocol.file.allow=always submodule update --remote
Submodule path 'lib/fmt': checked out '62d73c5214f3...'
$ git status -sb
## main
M lib/fmt
$ git diff --submodule
Submodule lib/fmt 003e5fd..62d73c5:
> fmt v2
$ git add lib/fmt && git commit -qm "Bump fmt to v2"
The parent’s change is a one-line pointer bump that you commit and push. Now the trap, from the teammate’s side. They run git pull and look at their status:
$ git pull -q
$ git status -sb
## main...origin/main
M lib/fmt
$ git submodule status
+003e5fda80685ddaaefc1cb06102420d1c6d2b00 lib/fmt (heads/main)
They pulled the new pointer, but their lib/fmt is still at the old commit. Git reports M lib/fmt, as if they had edited it, and + means the checkout doesn’t match the pointer. A careless git commit -a would revert the bump. The fix is git submodule update. Setting git config submodule.recurse true makes pull and checkout update submodules automatically.
Subtrees: copy it in
git subtree takes the opposite approach. It copies the other repository’s content into a folder as normal files and commits it:
$ git subtree add --prefix=vendor/fmt ../fmt.git main --squash
Added dir 'vendor/fmt'
$ git log --oneline --graph
* fa20142 Merge commit '13f3faf...' as 'vendor/fmt'
|\
| * 13f3faf Squashed 'vendor/fmt/' content from commit 62d73c5
* 8b2c55f Start jot
--squash collapses the other project’s history into one commit, so your history doesn’t absorb all of theirs. Updating, and sending local patches back, are explicit commands:
$ git subtree pull --prefix=vendor/fmt ../fmt.git main --squash
$ git subtree push --prefix=vendor/fmt ../fmt.git local-patch
* [new branch] 4223c2f... -> local-patch
I confirmed a clone of this repository gets vendor/fmt/fmt.sh straight away, with no .gitmodules and no extra commands. A subtree is invisible to collaborators.
Choosing
| Submodule | Subtree | |
|---|---|---|
| Stored as | pointer to a commit | ordinary files |
| Clone works out of the box | no | yes |
| Easy to see which upstream version | yes, exact hash | by reading the squash commit |
| Repository size | small | includes the code |
| Contributing back | edit inside the submodule, push there | git subtree push |
| Typical complaint | forgotten update --init |
noisy merges, harder to remember the commands |
Many teams skip both for libraries and use a package manager. Submodules and subtrees are for code you want in Git itself: shared tooling, vendored sources, content repositories.
What the video covers
Philomatics’ “Why everyone hates git submodules” is about eight minutes and was published around 2025. From its title it lists the pain points, which matches the traps above, and likely suggests alternatives. I judge English from the title, description and channel only, and I could not read a transcript, so I don’t know how much it says about subtree. The git subtree section is from my own run. LearnITGuide’s 2026 video, which appeared for both submodule and subtree searches, is linked as a runner-up.
Where this fits
Both techniques keep big projects manageable. The next lesson deals with big files: large files and Git LFS.
Check yourself
What does the parent repository actually store for a submodule?
A gitlink entry (mode 160000) with the submodule's commit hash, and `.gitmodules` with its path and URL. It doesn't store the submodule's files.
You cloned a project and `lib/fmt` is an empty folder. What went wrong?
A plain clone doesn't fetch submodules. Run `git submodule update --init`, or clone with `--recurse-submodules`.
After `git pull`, `git status` shows ` M lib/fmt` though you edited nothing. Why?
The pull moved the pointer to a newer submodule commit, but the folder is still checked out at the old one. `git submodule update` fixes it.
How does a clone treat a subtree differently from a submodule?
A subtree's files are ordinary tracked files in your history, so a plain clone has them. Nothing extra is needed, and there is no `.gitmodules`.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.