Sub topic 1
Why Embed Another Repository, and the Options Available
Prerequisites
This chapter assumes you already know the following:
- Everyday Git: commit, branch, merge, push, and pull.
- What a remote is, and that a commit hash identifies one exact snapshot of a repository.
- Basic command-line use.
You don't need any prior experience with submodules or subtrees. This chapter explains why you might embed one repository inside another and compares the options. Later chapters cover the commands in detail.
The problem: code that lives in someone else's repository
Most projects depend on code they don't own. That code might be:
- A library your team maintains in a separate repository and shares across several products.
- A third-party project you need to patch or pin to a known version.
- Shared assets such as schemas, protocol definitions, or design tokens.
- A theme, plugin, or configuration set that must sit at a fixed path inside your tree.
Each of these has the same core need. Your repository must contain, or point to, a specific version of another repository's content. A build from your repository must give the same result next month as it does today.
Git provides two built-in ways to do this: submodules and subtrees. Before choosing between them, check whether you need to embed anything at all.
Option zero: don't embed
Embedding is not the default answer. A package manager is often better.
| Approach | What you get | Typical fit |
|---|---|---|
| Package manager (npm, pip, Maven, Cargo, Go modules) | Versioned, published releases resolved by a lockfile | Libraries that publish releases |
| Copy the files by hand | Simplicity, and no link to upstream | One-off snippets you will never update |
| Submodule | A pointer to an exact commit in another repository | You need the full upstream repository at a pinned commit |
| Subtree | The other project's files, and optionally its history, inside your repository | You want a self-contained repository that still exchanges changes with upstream |
Use a package manager when the dependency publishes versioned releases and your language ecosystem supports it. You get version ranges, lockfiles, and checksums without any Git-specific steps.
Embed a repository when one of these holds:
- The dependency isn't published to a package registry.
- You need the source tree itself, not a built artifact. Examples are vendored C code, firmware, or shared protobuf files.
- You routinely change the dependency alongside your own code.
- Your build must work with no network access to a registry.
- The code is not tied to one language ecosystem.
The trade-offs of embedding
Embedding gives you control. It also adds work that a package manager would have handled.
Benefits:
- Exact reproducibility. Your repository records which version of the other code it uses.
- Source-level access. You can read, debug, and patch the dependency in place.
- Atomic coordination. A change to your code and the matching change to the dependency can be tracked together.
Costs:
- Extra commands. Cloning, updating, and publishing now involve steps that ordinary Git workflows don't have.
- Two histories to reason about. You must keep track of which repository a change belongs to.
- Easy mistakes. Forgetting a step can leave collaborators with a broken checkout. The most common case is a reference to a commit that was never pushed.
- Update discipline. Nothing updates the dependency for you. Someone must decide when and how to move to a newer version.
Neither option removes these costs. They differ in where the cost lands: on every developer's routine commands, or on the repository's size and history.
Option one: submodules
A submodule is a Git repository nested inside another Git repository. The outer repository is the superproject (also called the parent). The superproject does not store the submodule's files or history. It stores two small pieces of information:
- A gitlink: a special tree entry that records the exact commit hash of the submodule that the superproject uses.
- A
.gitmodulesfile: a plain text file in the superproject that maps the submodule's path to the URL it is cloned from.
The submodule's own history lives in its own repository. Git keeps a local clone of it inside the superproject's .git/modules directory.
The diagram below shows how a superproject and a submodule relate.
Key properties of submodules:
- The superproject always points to a specific commit, never to a branch. Moving to newer upstream code is a deliberate act that ends in a new commit in the superproject.
- The superproject stays small, because it doesn't contain the dependency's history.
- Collaborators must fetch the submodule content as an extra step after cloning.
- The submodule keeps its own identity. You can enter its directory and run Git commands there, and Git treats it as a separate repository.
- Contributing back is natural. You commit and push inside the submodule as you would in any repository, and then update the pointer in the superproject.
Submodules suit cases where the dependency is large, independently versioned, or developed by another team, and where precise pinning matters more than convenience.
Option two: subtrees
A subtree takes a different approach. The other project's files are copied into a directory in your repository, and they become ordinary tracked files. There is no special entry type and no extra metadata file that collaborators must understand.
The command git subtree helps you manage this arrangement. It can import an upstream repository into a directory, merge later upstream changes into that directory, and send your local changes back to upstream.
The following diagram shows the flow of content.
Key properties of subtrees:
- A normal
git clonegives everyone the full working tree. Nobody needs extra commands to get the dependency. - Your repository contains the dependency's files. It is therefore larger, and with full history imported it grows by the dependency's entire history. The
--squashoption keeps that growth down by collapsing upstream history into a single commit. - Day-to-day work needs no special knowledge. Developers edit files under
vendor/libfoolike any others. - The link to upstream is not stored as a distinct object. You supply the upstream URL and branch each time you pull or push, unless you record them in a script, an alias, or a remote.
- Pushing changes back is possible, but it relies on Git analyzing your history to find which commits touched the directory.
Subtrees suit cases where you want a simple experience for most contributors and only a few maintainers need to synchronize with upstream.
Comparing the two at a glance
| Question | Submodule | Subtree |
|---|---|---|
| What does the parent store? | A commit pointer and a URL | The actual files, and optionally upstream history |
| Does a plain `git clone` fetch the dependency? | No. Extra steps are required | Yes |
| Repository size | Small | Larger |
| Can you tell exactly which upstream version you use? | Yes, from the gitlink | Only if you record it, for example in commit messages from `--squash` |
| Extra Git knowledge for all contributors | Yes | Mostly no |
| Can you edit the dependency in place? | Yes, in its own repository | Yes, as ordinary files |
| Contributing changes upstream | Commit and push inside the submodule | `git subtree push` |
| Typical failure | A commit pointer to something that was never pushed | A hard-to-read merge history, or a slow `push` or `split` |
What can go wrong in each approach
Knowing the failure modes up front helps you choose.
Submodule failure modes:
- A collaborator clones the superproject and finds an empty directory where the submodule should be. A plain clone doesn't fetch submodule content.
- You commit a new pointer in the superproject but forget to push the submodule's commit. Others then see a commit hash that doesn't exist on the remote, and their update fails.
- Switching branches in the superproject doesn't automatically switch the submodule's checkout. The submodule can be left at a commit that doesn't match what the branch expects.
- A submodule checkout is normally in a detached HEAD state, which means it is on a commit and not on a branch. Commits made there are easy to lose if you don't create a branch first.
Subtree failure modes:
- Using
git subtree pullwithout--squashin one place and with it in another can produce confusing history. - Local edits to the vendored files and upstream changes to the same lines produce ordinary merge conflicts, and these appear in your own repository.
- Splitting or pushing a directory with a long history can be slow, because Git walks the history to find relevant commits.
- Because upstream version information isn't stored in a structured way, it is easy to lose track of which upstream version you have.
A decision sketch
The later chapters of this tutorial return to this decision with the full command knowledge in hand. For now, use these questions as a first pass.
- Does a package manager already handle this dependency? If yes, use it.
- Must every contributor be able to clone and build with no extra steps? If yes, lean toward subtree.
- Do you need to know and enforce the exact upstream commit? If yes, lean toward submodule.
- Is the dependency very large or does it have a long history? If yes, lean toward submodule.
- Does your team already work comfortably with several repositories at once? If yes, submodules cost less.
What comes next
The next chapters follow this order. First comes how submodules work internally and the daily commands: add, clone, update, edit, configure, and remove. Then comes how subtrees work, with adding, pulling, pushing, and splitting. The last chapter returns to the comparison and helps you choose for a specific team.
Exercise
Your team owns a shared set of protocol definition files in their own repository. Three products use them, and developers often change the definitions and a product in the same week. The files are not published to any package registry. Name one reason to embed this repository rather than use a package manager, and one cost your team accepts by embedding.
Show answer
Reason: the files aren't published to a registry, and developers need source-level access to change them alongside product code. Cost: contributors must learn extra Git steps, and someone must deliberately update the embedded copy, because neither submodules nor subtrees update themselves. With submodules, a forgotten push of the submodule's commit can also leave collaborators with a broken checkout.
Exercise
A new teammate clones your superproject with a plain git clone and finds the vendor/libfoo directory empty. Does this point to a submodule or a subtree setup, and why?
Show answer
A submodule setup. The superproject stores only a gitlink (a commit pointer) and the URL in .gitmodules, not the files. A plain clone doesn't fetch submodule content, so the directory exists but is empty. With a subtree, the files are ordinary tracked files and arrive with the clone.
Exercise
Which approach lets you read the exact upstream commit you use straight from the repository's structure, and which one stores the upstream files as normal tracked content?
Show answer
Submodules record the exact upstream commit in the gitlink entry. Subtrees store the upstream files as normal tracked content in your repository.