Git Subtree (original) (raw)

Last Updated : 7 May, 2026

Git Subtree is a strategy for including one Git repository as a subdirectory within another repository, allowing full integration of external code.

gitSubTrees

Adding a Subtree to Parent Repository

To add a subtree to a parent repository, first add the remote, then use the subtree command.

git remote add remote-name
git subtree add --prefix=folder/ remote-name subtree-branchname

The entire commit history of the child repository is merged into the main repository.

Using --squash

git subtree add --prefix= --squash

**Example:

git subtree add --prefix=subtreeDirectory https://github.com/microsoft/hermes-windows master --squashPush

Copies the external repository into subtreeDirectory.

Push Changes to Subtree

Use the following command to push local subtree changes to the remote repository:

git subtree push-all

Pull Changes from Subtree

Use the following command to fetch and merge updates from the remote subtree:

git subtree pull-all

Replace add with pull to update existing subtree:

git subtree pull --prefix= --squash

Benefits

Disadvantages

Git subtrees can introduce complexity in tracking and managing integrated repositories.

Git Subtree Vs Git Submodule

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.