Git: Subtree basics

If you hate git submodule, then you may want to give git subtree a try.

Let's say you have a git repository init'd with at least one commit.

You can add another repository to this respository like this:

  1. First specify you want to add the subtree
  2. Then specify the prefix directory into which you want to pull
  3. Specify the remote repository
  4. Specify the remote branch
  5. Specify you want to squash all the remote repository's logs

In other words

git subtree add --prefix .vimrepo master --squash

This will clone into the directory .vimrepo.

If you want to pull in any new commits to the subtree from the remote, issue the same command as above, replacing add for pull:

git subtree pull --prefix .vimrepo master --squash

If you make a change to anything in .vimrepo the commit will be stored in the host repository and its logs.

That is the biggest change from submodules.

If you now want to update the subtree repository with that commit, you must run the came command as above, excluding --squash and replacing pull for push.

git subtree push --prefix .vimrepo master

I've only just starting playing, but my main problems so far:

  1. You can't easily list the subtrees in your project
  2. You can't, at least easily, list the remote repositories of the subtrees
  3. The logs are slightly confusing when you update the host repository with subtree commits, then push the subtree to its host, and then pull the subtree.

Other than that, they're looking nicer than submodules.

git git-subtree

Edit on github
comments powered by Disqus
Click me