Skip to content

Instantly share code, notes, and snippets.

@dabbott
Last active February 14, 2019 20:44
Show Gist options
  • Save dabbott/8c23535ea8480ec003d319384deada34 to your computer and use it in GitHub Desktop.
Save dabbott/8c23535ea8480ec003d319384deada34 to your computer and use it in GitHub Desktop.
Working with Git Subtrees

The easiest way to add a module to a git repo, while still being able to develop the module as a standalone library, is with git subtree.

Adding a subtree

To do this, first set up the sub repo as a remote (let's call the remote foo):

git remote add foo https://github.com/hello/world.git

Next, add the sub repo as a subtree into the hello-world directory (which will be created automatically) in the root of the main repo:

git subtree add --prefix hello-world foo master --squash

That's it! If you don't need to sync with foo upstream, you don't need to do anything else.

Syncing with foo

To pull in new changes from foo, run:

git subtree pull --prefix=hello-world foo master --squash

To push new changes to foo, run:

git subtree push --prefix=hello-world foo master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment