Skip to content

Instantly share code, notes, and snippets.

@jspiro
Last active July 23, 2020 09:13
Show Gist options
  • Save jspiro/8fb9288b3294b3efc17e0c77a0282e2f to your computer and use it in GitHub Desktop.
Save jspiro/8fb9288b3294b3efc17e0c77a0282e2f to your computer and use it in GitHub Desktop.
Migrate repo AND select branches/PRs into a monorepo WITHOUT retaining history

Preparation

  • Source repo should have as many branches and PRs closed or merged as possible
  • Rest of PRs should be merged with master just before migration begins

Prepare the initial commit of the migrated repo (master)

  1. Archive source repo
  2. Go to target directory in monorepo
  3. Clone source repo into correctly named folder in monorepo
  4. cd to repo and make all necessary changes for migration to monorepo (e.g. renaming)
  5. Commit all changes INSIDE the cloned repo on local master (commit name doesn't matter, DO NOT PUSH)
  6. mv .git /tmp/reponame.git
  7. git add -A .
  8. git commit -m "migrate repo to monorepo"

Copy remaining branches/PRs individually (repeat this cycle for each) 0. cd to the source repo directory in the monorepo

  1. mv /tmp/reponame.git .git (switch git context back to source repo)
  2. git checkout --force PR-123 (switch to PR branch)
  3. git merge master and solve merge conflicts, and add all changed files (merge in changes from step 4 above)
  4. mv .git /tmp/reponame.git (switch git context back to monorepo)
  5. git checkout -b reponame/PR-123 (create a new branch in monorepo to store this PR)
  6. git add --all --force . (add all changes, including ignored files)
  7. git status (double-check all files were added and removed correctly)
  8. git commit -m "Original PR title (Repo#123)" (create a squash commit for this PR)
  9. gh pr create (create and push a PR for this branch)
  10. git checkout --force - (go back to original branch in monorepo)
  11. git reset --hard HEAD (clear out any lingering changes)
  12. mv /tmp/reponame.git .git (switch git context back to source repo)
  13. git checkout --force master (clear out any lingering changes)
  14. mv .git /tmp/reponame.git (switch git context back to monorepo)
  15. Repeat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment