Skip to main content
Chapter 27 of 61 · 1 sections · Page 122

Git: Subtrees

Embed third-party repositories, pull updates, and backport commits using git subtree.

Original chapter reference: Chapter 27: Subtrees. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Section 27.1: Create, Pull, and Backport Subtree

Create Subtree

Add a new remote called plugin pointing to the plugin's repository:

Source example · PDF page 122
git remote add plugin https://path.to/remotes/plugin.git

Then Create a subtree specifying the new folder prefix plugins/demo. plugin is the remote name, and master refers to the master branch on the subtree's repository:

Source example · PDF page 122
git subtree add --prefix=plugins/demo plugin master

Pull Subtree Updates

Pull normal commits made in plugin:

Source example · PDF page 122
git subtree pull --prefix=plugins/demo plugin master

Backport Subtree Updates

1. Specify commits made in superproject to be backported:

Source example · PDF page 122
      git commit -am "new changes to be backported"

2. Checkout new branch for merging, set to track subtree repository:

Source example · PDF page 122
      git checkout -b backport plugin/master

3. Cherry-pick backports:

Source example · PDF page 122
      git cherry-pick -x --strategy=subtree master

4. Push changes back to plugin source:

Source example · PDF page 122
      git push plugin backport:master
Buy me a coffee
Daily Practice
0/0