Skip to content

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

git clone cd your_repo
Code language: Bash (bash)

Step 2: Checkout the branch where the subdirectory is

git checkout your_branch_name
Code language: Bash (bash)

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:

git filter-branch --prune-empty --subdirectory-filter relative/path/to/subdirectory your_current_branch_name
Code language: Bash (bash)

Step 4: Update your new Git Remote

At this stage, you can go and create a new git repository, and copy the path:

git remote set-url origin
Code language: Bash (bash)

Step 5: Push your changes

You can now push your changes to your new repository:

git push -u origin your_current_branch_name
Code language: Bash (bash)

As a runnable script

git clone cd your_repo git checkout your_branch_name git filter-branch --prune-empty --subdirectory-filter relative/path/to/subdirectory your_current_branch_name git remote set-url origin git push -u origin your_current_branch_name
Code language: Bash (bash)

See also  [Solved] fatal: unable to access : SSL certificate problem: self signed certificate in certificate chain
Notify of
Oldest Most Voted
Inline Feedbacks
View all comments
2 months ago


Olexander Vochkov
Olexander Vochkov
8 months ago

Thanks! Very useful tips!
By the way – you need to delete all tags that pointed to needless commits also.
On Linux you just can run:
git tag | xargs git tag -d

Tyler mace
1 year ago

Given that the files repository might have LFS pointers to files in them, it might warrant an additional couple of steps. This is warranted even if there are no LFS-based files in your target directory but other directories.

git lfs migrate export –everything –include .                                     
git commit -a -m “Result of flattening LFS pointers to files.”

Do this step BEFORE the filter-branch step.

5 months ago
Reply to  Tyler mace

I have a old repo with multiple directories and separating the sub directories to its own new repos with the git history, old repository have LFS pointers to files in them,


i need to move test to its own test-repo with the history, could you please give me the steps

Would love your thoughts, please comment.x