How to break up a monorepo using git subtree:
- Set up some basic stuff:
- These should be independent, not nested
$ NEW_REPO_PATH=
$ OLD_REPO_PATH=
- isolate the files inside a branch
- make your preferred file tree
- choose a folder to isolate (e.g. my-cool-crate)
- for convenience, you can make the branch name the same as the folder :)
$ FOLDER_TO_ISOLATE=my-cool-crate
$ git subtree split -P $FOLDER_TO_ISOLATE -b $FOLDER_TO_ISOLATE
- add the old monorepo as a remote in your new repo
$ mkdir -p $NEW_REPO_PATH
$ cd $NEW_REPO_PATH
$ git init
$ git remote add old_repo $OLD_REPO_PATH
- Pull the monorepo branch into your new repo on main
$ git pull old_repo $FOLDER_TO_ISOLATE
- Push the contents to github
$ git push -u origin main
- Clean up the isolated branch
$ cd $OLD_REPO
$ git branch -D $FOLDER_TO_ISOLATE
to port a PR easily:
-
perform the steps above to create the new isolated repo
-
check out your PR branch
$ cd $OLD_REPO_PATH
$ PR_BRANCH=prestwich/my-pr-branch
$ git checkout $PR_BRANCH
- push the subtree to the new repo
- the arguments are isolated folder name, new repo location, branch name
$ git subtree push --prefix $FOLDER_TO_ISOLATE $NEW_REPO_PATH $PR_BRANCH
- Push the new branch to the new github repo
$ cd $NEW_REPO_PATH
$ git switch $PR_BRANCH
$ git push -u origin $PR_BRANCH
- Open the PR on in the new repo on github as normal