Skip to content

Moving a subdirectory to a new git repo

Amin Bandali edited this page Oct 2, 2016 · 1 revision

To move a subdirectory of a git repository into a new repo, conserving as much history as possible (keeping related commits), we'll use git subtree.

First, open up a terminal and browse to the source repo:

cd literate-unitb

Let's say the directory we want to move to a new repo is libs/invariants.

We tell git to create a new branch (e.g. called amin/separate-invariants) from the split subtree, that will contain only the subdir we specify:

git subtree split -P libs/invariants -b amin/separate-invariants

Now, we go outside the current repo, make a new folder an initialize a new git repo:

cd ..
mkdir invariants
cd invariants
git init

And finally, we pull the new branch from the source repo into the new repo's master:

git pull ../literate-unitb amin/separate-invariants

That's it!

Clone this wiki locally