In this lab, you’ll get some experience working with subtrees.
PREREQUISITES
This lab requires that you have Internet access and have completed at least the first two steps in Connected Lab 10, where you forked the various split projects of the original calc2 project into your area in GitHub and cloned the super_calc project down to your local system.
STEPS
Start out in the super_calc directory for the super_calc repository that you cloned from your GitHub fork in Connected Lab 10. You’re going to add another repository as a subtree to super_calc.
Even though you don’t have much history in this repository, you used the --squash command to compress it. Note that the -P stands for prefix, which is the name your subdirectory gets.
Look at the directory structure; note that the sub_docs subdirectory is there under your super_calc project. Also, if you do a git log, you can see where the subproject was added and the history squashed.
$ ls sub_docs
$ git log --oneline
Note that there is only one set of history here because there is only one project effectively—even though we have added a repository as a subproject.
Now, you will see how to update a subproject that is included as a subtree when the remote repository is updated. First, clone the sub_docs project down into a different area.
$ cd ..
$ git clone https://github.com/<your github user id>/sub_docs sub_docs_temp
Change into the sub_docs_temp project, and create a simple file. Then stage it, commit it, and push it.
Go back to the super_calc project where you have sub_docs as a subtree.
$ cd ../super_calc
To simplify future updating of your subproject, add a remote reference for the subtree’s remote repository.
$ git remote add sub_docs_remote https://github.com/<your github user id>/sub_docs
You want to update your subtree project from the remote. To do this, you can use the following subtree pull command. Note that it’s similar to your add command, but with a few differences:
You used the long version of the prefix option.
You are using the remote reference you set up in the previous step.
You don’t have to use the squash option, but you add it as a good general practice.
Because this creates a merge commit, you will get prompted to enter a commit message in an editor session. You can add your own message or just exit the editor.
After the command from step 8 completes, you can see the new README file that you created in your subproject sub_docs. If you look at the log, you can also see another record for the squash and merge_commit that occurred.
$ ls sub_docs
$ git log --oneline
Changes you make locally in the subproject can be promoted the same way using the subtree push command. Change into the subproject, make a simple change to your new README file, and then stage and commit it.
Note the similarity between the form of the subtree push command and the other subtree commands you’ve used.
The next few steps show you how to take a subproject, put it onto a different branch, and then bring that content into a separate repository. First, use the subtree split command to take the content from the sub_docs subproject and put it into a branch named docs_branch in the super_calc area.