![]() |
VOOZH | about |
Git Subtree is a strategy for including one Git repository as a subdirectory within another repository, allowing full integration of external code.
To add a subtree to a parent repository, first add the remote, then use the subtree command.
git remote add remote-name <URL to Git repo>
git subtree add --prefix=folder/ remote-name <URL to Git repo> subtree-branchname
The entire commit history of the child repository is merged into the main repository.
git subtree add --prefix=<local-directory> <repo-URL> <branch> --squashExample:
git subtree add --prefix=subtreeDirectory https://github.com/microsoft/hermes-windows master --squashPushCopies the external repository into subtreeDirectory.
Use the following command to push local subtree changes to the remote repository:
git subtree push-allUse the following command to fetch and merge updates from the remote subtree:
git subtree pull-allReplace add with pull to update existing subtree:
git subtree pull --prefix=<directory> <repo-URL> <branch> --squashGit subtrees can introduce complexity in tracking and managing integrated repositories.
Git subtree and submodule are two approaches to include external repositories, differing in integration, storage, and workflow.
| Git Subtree | Git Submodule |
|---|---|
| Repository is stored as a subdirectory inside the main project. | Repository is stored as a separate linked repository. |
| Simple no extra commands after cloning. | Requires git submodule init and git submodule update. |
| Clones everything in one go. | Needs an additional step to fetch submodules. |
| Fully integrated — subtree files are part of main repo history. | Submodule files are not part of main repo history. |
| Works completely offline. | May need internet access to update submodules. |
| Subtree commits are merged into main repo commits. | Submodule stores only a reference (commit ID) of the external repo. |
| Easier to understand and manage. | More complex to maintain and sync. |
| Team members don’t need special setup. | Every collaborator must initialize submodules manually. |
| Best when simplicity and integration are priorities. | Best when keeping repositories strictly separate is important. |