Subdirectory to Submodule
Goal: Split a subfolder (e.g. a Python package) from an existing (Go) project into a standalone repository with a fully preserved commit history.
This documents how I split ytx into a separate fastapi project.
Prerequisites
Section titled “Prerequisites”Install git-filter-repo
:
brew install git-filter-repo # macOS orpip install git-filter-repo # cross-platform
Clone the Original
Section titled “Clone the Original”git clone https://github.com/desertthunder/ytx.gitcd ytm
Filter the history to only include the subdirectory
Section titled “Filter the history to only include the subdirectory”git filter-repo --path music --forcegit mv music/**/* ./git commit -m "unwrap repo"
This rewrites history so only commits touching that folder remain.
git remote add origin git@github.com:desertthunder/ytm.gitgit push -u origin main
Linking Back
Section titled “Linking Back”In the original project:
cd ytx # clone ytxgit rm -r --cached musicrm -rf musicgit submodule add git@github.com:desertthunder/ytm.git musicgit commit -m "Add Python package as submodule"