Git Subtrees

In this page

  1. Basics of git subtree

1. Basics of git subtree


Start by creating a repository for the library folder. Make sure that the library is in this folder and absent from the project.

Go to the library repository and check its remote location, called from this point on lib_loc. The command to check the remote location is:

$ git remote -v
Create local copy of library

Now lets create a local copy of the library on the project repository. This copy will be put in a particular folder, which I am calling lib and this folder alone will talk to one of the library repository branches. The command that does it all, linking the folder lib to the master branch of lib_loc remote repository:

$ git subtree add --prefix lib lib_loc master --squash
Talking to the library

To avoid typing the long name for the remote library repository, let's first create an alias (mylib) for it:

$ git remote add -f mylib lib_loc

And now you can pull from and push to mylib:

$ git subtree pull --prefix lib mylib master --squash
$ git subtree push --prefix lib mylib master --squash