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
Where
is the folder containing the subproject's source code.
</li> </ol>
Update the Maven build
Assuming you are using Maven to build the subproject:
- Add an *
* section to the *pom.xml* to reflect the new remote repository's URL (see [example](https://github.com/scijava/jep/commit/b76f4a1df830c090fc96ab99bb145dd67e8e69ce)): vi pom.xml git commit -m 'Add SCM location' pom.xml - Replace the old *
* with a new one, such as [pom-scijava](https://github.com/scijava/pom-scijava), [pom-imagej](https://github.com/imagej/pom-imagej) or [pom-fiji](https://github.com/fiji/pom-fiji) (see [example](https://github.com/scijava/jep/commit/336c0a46fad855508aaa905a9f82e5d88136df91))—or remove the * * altogether. - Add a *
* section to the *pom.xml* to indicate the project developers (see [example](https://github.com/fiji/TrackMate/commit/f0c2cf6cca3e198ba5b9283a71fc564f41c642d5)). You can also add * * 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 status
andgit diff
every 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 git@github.com:my-org/my-new-project
Where git@github.com: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