Skip to content

Instantly share code, notes, and snippets.

@prestwich
Last active November 25, 2022 17:15
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prestwich/afb540efce1d3995a5875a27579f5831 to your computer and use it in GitHub Desktop.
Save prestwich/afb540efce1d3995a5875a27579f5831 to your computer and use it in GitHub Desktop.
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
$ NEW_REPO_PATH=
$ OLD_REPO_PATH=
  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
$ cd $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

$ cd $OLD_REPO_PATH
$ 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
$ cd $NEW_REPO_PATH
$ 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