Skip to main content

About Git subtree merges

If you need to manage multiple projects within a single repository, you can use a subtree merge to handle all the references.

Platform navigation

About subtree merges

Typically, a subtree merge is used to contain a repository within a repository. The "subrepository" is stored in a folder of the main repository.

The best way to explain subtree merges is to show by example. We will:

  • Make an empty repository called test that represents our project.
  • Merge another repository into it as a subtree called Spoon-Knife.
  • The test project will use that subproject as if it were part of the same repository.
  • Fetch updates from Spoon-Knife into our test project.

Setting up the empty repository for a subtree merge

  1. Open TerminalTerminalGit Bash.

  2. Create a new directory and navigate to it.

    mkdir test
    cd test
    
  3. Initialize a new Git repository.

    $ git init
    > Initialized empty Git repository in /Users/octocat/tmp/test/.git/
    
  4. Create and commit a new file.

    $ touch .gitignore
    $ git add .gitignore
    $ git commit -m "initial commit"
    > [main (root-commit) 3146c2a] initial commit
    >