Sub topic 1
Why Embed Another Repository, and the Options Available
Prerequisites
This chapter assumes you are comfortable with everyday Git: commit, branch, merge, push, and pull. You should know what a remote is and what a commit hash identifies. You should also be able to work at a command line. No submodule or subtree experience is needed.
Why embed another repository
Many projects depend on code that lives in another repository. Common cases include:
- A shared library that several applications use.
- A vendored third-party dependency that you need at a specific version.
- Documentation, themes, or configuration that is maintained separately.
- A component that your team also develops, and that you want to change alongside your main project.
You could copy the files in by hand, but then you lose the link to the upstream history. You cannot easily tell which version you have, and pulling in fixes becomes manual work. Git offers two built-in ways to keep that link: submodules and subtrees.
A package manager such as npm, pip, or Maven is a third option. It is often the best choice when the dependency is published as a package. This tutorial covers the cases where a package manager does not fit, for example when you need the source in your tree or you edit the dependency yourself.
The two built-in options
A submodule stores a pointer to an exact commit in another repository. Your repository does not contain the other project's files or history. It records where to fetch them and which commit to use.
A subtree copies the other project's files into a directory of your repository. The files become ordinary tracked content. Git can merge upstream changes into that directory later, and can send your local changes back.
Comparing the models
| Aspect | Submodule | Subtree |
|---|---|---|
| What the parent stores | A gitlink (commit pointer) and a `.gitmodules` file | The files themselves, as normal commits |
| Parent repository size | Small | Grows with the embedded code |
| Cloning | Needs an extra step to fetch the submodule | Works with a plain clone |
| Version pinning | Exact commit, explicit | The merged state, recorded in history |
| Extra tooling for collaborators | They must know submodule commands | Mostly none for reading and editing |
Neither option is better in every case. The trade-offs are the subject of the rest of this tutorial, and the final chapter returns to the decision.
Trade-offs of embedding at all
Embedding gives you control and reproducibility. A build at a given parent commit always uses the same version of the dependency. The cost is maintenance. You have to update the embedded code on purpose, and you have to decide how to handle local changes to it.
Two common problems follow from this:
- Drift. The embedded copy falls behind upstream, and nobody notices until a security fix is missed.
- Divergence. Someone edits the embedded code locally and the changes never reach upstream, so each update becomes a merge conflict.
What comes next
The next chapters follow the submodule path first: how gitlinks and .gitmodules work, then adding, cloning, updating, editing, configuring, and removing a submodule. After that, the subtree path covers how subtrees work, adding and pulling, and pushing and splitting. The last chapter compares the two and gives decision criteria.
Exercise
Your team wants to use a shared logging library in three applications. The library is not published to a package registry, and developers sometimes need to patch it. Name one reason to embed it with a Git mechanism rather than copying the files by hand.
Show answer
Embedding keeps a link to the library's history. You can see which version each application uses, pull in upstream fixes, and send your patches back, none of which a manual copy supports.
Exercise
Which option adds the other project's files to your repository as ordinary tracked content, and which one stores only a pointer to a commit?
Show answer
A subtree adds the files as ordinary tracked content. A submodule stores only a pointer (a gitlink) to a commit in the external repository.