a3a5xg
Last Updated: May 15, 2018
·
4.503K
· janosgyerik
32ef4e3e388cbadc756a008cade3ee6a

Splitting a project sub-directory to a new Git repo

git

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:

http://git-scm.com/book/en/Git-Tools-Submodules

Say Thanks
Respond

4 Responses
Add your response

16136
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 ·
16155
32ef4e3e388cbadc756a008cade3ee6a

@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 ·
17544
None

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

over 1 year ago ·
29909

How can we achieve same task using only Subtree(Not submodule)? Currently I am working on android project where I have created base project in one repository. I want to use one of directory (package) from this project as a core for other projects in different repository. I want to create subtree of this package to avoid copy-pasting but don't know how as java uses package structure for projects.

5 days ago ·
Filed Under