Git Subtree and Dependency Vendoring
Copy content rather than pin a separate checkout
A subtree import places dependency files at a prefix in the parent as ordinary tracked files. A normal clone includes them. git subtree is a contributed Git command which may not be installed with every distribution; check availability before using it. The --squash mode imports an aggregated snapshot change rather than joining every source commit into the parent ancestry. Without squash the source history can be joined. Vendoring more generally means copying reviewed dependency files into the project; it does not require git subtree. Keep licensing and provenance records.
Import a reviewed version
In the separate vendor-app fixture, the trusted library path is ../plants. The prefix must not already exist and the parent should be clean. Use the tag identifying version A, not a moving branch tip. The command imports source files and creates commits; inspect the new tree and preserve license notices. The source repository remains independent and unchanged.
git subtree add --prefix=vendor/plants --squash ../plants lib-v1git ls-tree -r HEAD -- vendor/plantsgit show HEAD:vendor/plants/plant.txtUpgrade and verify
Use the same prefix and consistent squash strategy when upgrading to the reviewed version B. Inspect the parent diff and compare the imported content to the source snapshot. Local modifications can cause conflicts; preserve work and resolve them with tests rather than replacing a directory blindly. A subtree split can derive a separate history of a prefix, but round-trip contribution and publishing need an explicit team policy. Manual vendoring also needs an update process and vulnerability tracking.
git subtree pull --prefix=vendor/plants --squash ../plants lib-v2git show HEAD:vendor/plants/plant.txtgit diff HEAD^ HEAD -- vendor/plantsPractice
Choose between submodule and subtree for a consumer that must work from a normal clone without a second checkout. Subtree is a reasonable choice if licenses allow copying and the team owns updates. For independent versioned history and explicit pins, a submodule may fit better. Neither approach automatically audits security or license compliance.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Subtree and Dependency Vendoring — knowledge check
Reason about dependency revisions, ownership and repository boundaries.
What does a subtree import store at its prefix?
What is the purpose of --squash during subtree import?
Which command updates a reviewed subtree prefix?
What responsibility remains after vendoring?
A consumer must clone once and receive dependency files. Which is a reasonable option?