Basic git subtrees

January 21, 2022

Condensed notes on how to use git subtrees to vendor in source code.

Basics (add, pull, push)

Pull in a new repository to dir/:

$ git subtree add --prefix dir/ REMOTE REMOTE-BRANCH --squash

The squash flag can be elided if the subtrees history is wanted.

To pull in updates:

$ git subtree pull --prefix dir/ REMOTE REMOTE-BRANCH --squash

If changes need to be pushed back upstream

$ git subtree push --prefix dir/ REMOTE REMOTE-BRANCH --squash

A git subtree is just a folder, so can be removed with rm.

Splitting off a subtree

Split off a directory's commits in a separate branch

$ git subtree split -P dir/ -b NEW-BRANCH-NAME

This can then be pushed to the remote:


The old directory can be left in place, and the subtree pull commands can be used as per-normal.