How To Install Git Subtree on Mac and Ubuntu

Git subtree is a git helper to combine and manage multiple repos in one project. It's similar to submodules but allows you to share commits and content between repos rather than pointers to install other repos. Many people use subtree as a replacement for submodules. But, this post isn't about how great subtree is or an into to using it. If you want that you can read the documentation or one of the nice writeups. Instead, this post is simply about installing subtree on Mac and Ubuntu.

Mac (OS X 10.8)

There are a couple ways to install git subtree on mac depending on how you installed git on your system.

With Homebrew

If you used homebrew to install git then subtree, along with the rest of the git contrib items, was already placed on your system and can be installed. To install subtree:

  1. Fire up a terminal and go to /usr/local/share/git-core/contrib/subtree.
  2. Run make which will prepare subtree.
  3. Run make prefix=/usr/local/opt/git/ install. The prefix is important because the default location the makefile knows about is not where it needs to be installed with homebrew.

Git From Installer

If you downloaded and installed git using the installer from the git website there is a different method to installing git-subtree:

  1. Since git contrib was not put on your system you'll need to checkout the git source. Don't worry about compiling or installing git. You just need access to the contrib director to install subtree (which is mostly shell scripts).
  2. In a terminal go into the git/contrib/subtree directory.
  3. Run make to prepare subtree.
  4. Run sudo make prefix=/usr install. The prefix is important for it to be installed in the right location. Note, you need to use sudo to install this because of it's location on the system.
  5. Remove the git source (unless you want to keep it around for another reason).

Ubuntu (12.04 and 12.10)

Installing subtree on Ubuntu has been the most challenging environment I've tried it on. That said, it still only takes a few minutes to do so.

I'm assuming apt-get was used to install git. In this case the most recent version of git would have been 1.7.10. Git 1.7.11 was where subtree was moved to contrib. Since subtree is mostly shell scripts the version of subtree included in the latest git (1.8.0.2 at the time of this writing) works just fine with git 1.7.10.

The steps to install subtree:

  1. Checkout the git source as we need to get to the contrib directory. There will be no need to compile git itself.
  2. Instide a shell go into the git/contrib/subtree directory.
  3. Run make to prepare subtree.
  4. The Makefile makes some assumptions about where git will be installed that aren't correct for ubuntu. So, running make install will show an error. Instead run sudo install -m 755 git-subtree /usr/lib/git-core to install subtree.
  5. Remove the git source (unless you want to keep it around for another reason).