Skip to content

Instantly share code, notes, and snippets.

Last active Nov 25, 2022
What would you like to do?
How to break up a monorepo :)

How to break up a monorepo using git subtree:

  1. Set up some basic stuff:
    • These should be independent, not nested
  1. 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
  1. add the old monorepo as a remote in your new repo
$ mkdir -p $NEW_REPO_PATH
$ git init
$ git remote add old_repo $OLD_REPO_PATH 
  1. Pull the monorepo branch into your new repo on main
$ git pull old_repo $FOLDER_TO_ISOLATE
  1. Push the contents to github
$ git push -u origin main
  1. Clean up the isolated branch
$ cd $OLD_REPO
$ git branch -D $FOLDER_TO_ISOLATE

to port a PR easily:

  1. perform the steps above to create the new isolated repo

  2. check out your PR branch

$ PR_BRANCH=prestwich/my-pr-branch
$ git checkout $PR_BRANCH
  1. 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
  1. Push the new branch to the new github repo
$ git switch $PR_BRANCH
$ git push -u origin $PR_BRANCH
  1. Open the PR on in the new repo on github as normal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment