Saturday, March 14, 2015

Git Subtrees vs. Git Submodules

Subtree's Strengths Over Submodule

  • Older versions of git are supported.
  • Subtree is less intrusive to other collaborators on the repository. They can flat out ignore that the subtree exists.
  • Fewer modifications to your workflow. You'll have to learn about a new merge strategy (i.e.subtree), but this requires you to learn fewer steps and commands that come along with submodule.
  • Avoids the deployment issues that come with submodule when the submodule is private and hosted on a private server.

One major drawback, however, is that you have to be careful not to mix the super and subtree code in your commits.

What Exactly is Git Subtree?

Subtree basically allows you to add a remote repo to your main repo which is the equivalent of cloning the remote. And from within the main repo, you have the ability to merge the master or any branch of another repo into a specified subdirectory or number of subdirectories. Also, if you make any changes to the files that are tracked in the subtree, you can actually push those changes to the remote repo's upstream.

Adding / Removing a Subtree

To add a subtree, perform the following steps.

# add a remote to repo
git remote add my-subtree

# add subtree
# path/for/subtree is where the subtree content should live
git subtree add --prefix=path/for/subtree repo-name branch

# remove subtree
git rm path/for/subtree

Pushing and Pulling to/from the Subtree

# push to subtree's repo
git subtree push --prefix=path/to-repo repo-name branch

# pull changes from subtree's repo back to main repo
git subtree pull --prefix=path/to-repo repo-name branch

No comments:

Post a Comment