Sometimes, a piece of functionality is developed as part of one project, but grows so much as to warrant becoming its own, separate project.
This tutorial describes how to split part of a Git repository into its own dedicated Git repository, preserving only the history relevant to the subproject being extracted.
Extract the revision history
Use Git’s filter-branch feature to extract the Git history of only the subproject:
git filter-branch -f --prune-empty --subdirectory-filter <subdir>
<subdir>is the folder containing the subproject’s source code.
Update the Maven build
Assuming you are using Maven to build the subproject:
Add an <scm> section to the pom.xml to reflect the new remote repository’s URL (see example):
vi pom.xml git commit -m 'Add SCM location' pom.xml
Add a <developers> section to the pom.xml to indicate the project developers (see example). You can also add <contributors> if desired and relevant.
Make sure the project still builds:
mvn clean package
Add (or adjust) the .gitignore file (see example).
Push the changes
Make sure that all your changes look good:
git status git diff
This is good advice in general: check
git diffevery time before you commit, to prevent making a fool out of yourself.
Commit everything, mentioning the commit of the parent project from which history was rewritten (see example):
git add . && git commit -s
Create a new repository somewhere for the new project—we recommend GitHub.
Connect your repository with the remote one:
git remote set-url origin email@example.com:my-org/my-new-project
Where firstname.lastname@example.org:my-org/my-new-project is the remote URL for the new project’s dedicated repository.
Push the resultant history to the project’s new repository:
git push -u origin master
Change any online resources
- Edit the relevant web page(s) to reflect the new Git repository location
- Update any other known links to the project