I worked on my thesis in one repo, let’s call it thesis. My lab has another repo, pubs, and we have a policy of keeping all submissions (final or in draft) in this repo.

I decided to use subtree merge to maintain the history of my own repo, while moving the contents into another repo. In other words, I grafted a subdirectory of one repo onto a subdirectory of another. For generality, suppose you want to move the path a/b on the repo OLD to the path y/z on the repo NEW.

First, use git subtree split to create a commit on repo OLD, containing only commits which affect the path a/b, putting the resulting commit on branch B:

# (inside repo OLD)
git subtree split -P a/b -b B

This results in the following output, the last line of which is the hash of the new commit. Remember that hash, as we’ll use it later when merging into the new repo.

Then switch to the repo NEW, and add the repo OLD as a remote:

git remote add -f old-remote PATH-TO-OLD

Issue the following command, where COMMIT is the commit hash we got in the subtree split:

# (inside repo NEW)
git subtree add --prefix=y/z COMMIT

The result is that the commit history of subdirectory a/b in the OLD repo is spliced onto subdirectory y/z of NEW, which is what we wanted.