Last Updated: February 25, 2016
· janosgyerik

Splitting a project sub-directory to a new Git repo


Recently I realized that a sub-directory in a Git project
would be better as an independent project.
Luckily this is such a common need,
Git has a nice command to make this easy called subtree split.
It creates a new branch in the project,
with only the commits that involved the specified sub-directory.

Let's say you have a project with a sub-directory called plugins/media and you want that in an independent repository:

git subtree split -P plugins/media -b media

This will put all commits related to plugins/media in a new branch named media. You can confirm the result with git log media.

Next, create the target repository, for example on GitHub,
or locally:

git init --bare /tmp/proj-media.git

Create a remote for the repository,
add a remote for it,
and push the media branch to the remote with the name `master:

git remote add proj-media /tmp/proj-media.git
git push proj-media media:master

Note that the original repository is unchanged:
the plugins/media directory still exists, untouched.
Most probably you want to replace the directory with the new project as a submodule:

git rm -r plugins/media
git submodule add url_to_repo plugins/media
git commit -m 'replaced plugins/media with a submodule'

For more on submodules, see this chapter in the Pro Git book:


Say Thanks

3 Responses
Add your response

Avatar big

I recently searched how to do that and I found an answer from the github help: https://help.github.com/articles/splitting-a-subfolder-out-into-a-new-repository

over 1 year ago ·

@gluchet that method is roughly equivalent, but it rewrites the current branch. My method is safer. I also added more tips: the exact steps to replace the directory with a submodule, and pushing to a remote. I also explain in more detail what's going on. But thanks, it's interesting to know other methods too!

over 1 year ago ·

Oh this is neat, might have to use this soon.

over 1 year ago ·
Filed Under