🔀 Git & GitHub Intermediate

What is git subtree?

Why Interviewers Ask This

Mid-level Git & GitHub roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

git subtree is an alternative to submodules for including one repository inside another. Instead of references (submodules), subtree merges the external repo's history directly into the parent repo. Adding a subtree: git subtree add --prefix=vendor/lib https://github.com/other/lib.git main --squash. Pulling updates: git subtree pull --prefix=vendor/lib https://github.com/other/lib.git main --squash. Pushing changes back: git subtree push --prefix=vendor/lib https://github.com/other/lib.git main. Advantages over submodules: (1) Normal clone works — no --recurse-submodules; (2) No .gitmodules complexity; (3) Contributors don't need to know it's a subtree; (4) History is preserved in the parent. Disadvantages: (1) Parent repo history becomes polluted with external repo commits; (2) Harder to see which code is "yours" vs external; (3) Pushing back is complex. Use cases: when you want simpler workflows than submodules; when the external code is mostly read (vendor libraries). In modern development, package managers (npm, pip, Maven) are preferred over both submodules and subtree for third-party dependencies.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Git & GitHub codebase.