Suppose you wanted to add the images/
prefix of the octocat git repo at the ref master
.
Suppose we want to use the remote hosted at https://github.com/octocat/octocat.github.io.git (Note: GitHub returns error: Server does not allow request for unadvertised object
in the following fetch
command is you specify a sha1 not associated with named ref)
Starting on the branch you wish to add the subtree to, let's first fetch the desired commit history and checkout our new branch
git fetch https://github.com/octocat/octocat.github.io.git master:tmp_octocat_master
git checkout tmp_octocat_master
Next, let's create a new history where only the files under our desired prefix (images/
) exist.
git subtree split --prefix=images -b subtree_split_branch
Finally, let's add this subtree to our desired branch (presumably the last branch you were on, git checkout -
)
git checkout -
git subtree add --prefix=public/images subtree_split_branch
Now you should have all the desired files on your current branch with a full git history.
Alt Squashed history
At time you want the commits in our subtree to be squashed into a single commit. This to a certain extent defeats the purposes of adding a subtree but it has it's place. The following is a variation of what is described above to limit the history pulled into your repo
Starting on the branch you wish to add the subtree to:
git fetch --depth=1 https://github.com/octocat/octocat.github.io.git master:tmp_octocat_master
git checkout tmp_octocat_master
Note: we are specifying --depth=1
in the above since we are using the --squash
is the following git subtree split
command.
git subtree split --squash --prefix=images -b subtree_split_branch
git checkout -
git subtree add --prefix=public/images subtree_split_branch
git fast-export
. – Dropkick