How to Split a Subdirectory to a New Git Repository and Keep the History
There comes a time when you need to split out a subdirectory into it’s own git repo.
This is a very simple task if you don’t care about persisting the git history for any changes that were made in that subdirectory.
However, if you would like to keep all the history of the subdirectory only, and not of the overall entire repository itself, then you can perform the following bunch of steps:
Step 1: Clone your existing repo to a temp location
Step 2: Checkout the branch where the subdirectory is
|
|
Step 3: Run the Git Filter-Branch Command
The git filter-branch
command allows you to prune
empty entries and specify a subdirectory filter to base off:
|
|
Step 4: Update your new Git Remote
At this stage, you can go and create a new git repository, and copy the path:
|
|
Step 5: Push your changes
You can now push your changes to your new repository:
|
|
As a runnable script
|
|