Skip to content

Instantly share code, notes, and snippets.

@idleberg
Last active June 13, 2026 12:13
Show Gist options
  • Select an option

  • Save idleberg/f8fcb1b262b3534ad190306f72f85768 to your computer and use it in GitHub Desktop.

Select an option

Save idleberg/f8fcb1b262b3534ad190306f72f85768 to your computer and use it in GitHub Desktop.
Move repository into a monorepo

This is a guide on how you can move a repository into an existing monorepo while keeping the entire history.

1. Add remote

First, let's define some variables:

REPO_ORIGIN=github.com
REPO_USER=idleberg
REPO_NAME=demo

Within the monorepo, add the repository you want to migrate as a remote:

git remote add $REPO_NAME git@$REPO_ORIGIN:$REPO_USER/$REPO_NAME.git
git fetch $REPO_NAME

Let's put the existing files from the monorepo to the side for the migration

git checkout --no-track -b migration/$REPO_NAME $REPO_NAME/main
git clean -dfx

2. Optional: Rename tags

Within the monorepo, you might want to use a new naming scheme for tags.

git tag -l | while read tag; do
  if git merge-base --is-ancestor "$tag" "$REPO_NAME/main" 2>/dev/null; then
    # You might want to edit the name for the new tag
    git tag "$REPO_NAME/$tag" "$tag"
    
    # Delete the old tag-name
    git tag -d "$tag"
  fi
done

3. Optional: Move files

You might want to move the files from the new remote to a new folder, e.g. NodeJS projects often have a workspace folder (e.g. packages)

4. Commit files

You can now commit all the changes

5. Merge histories

git checkout --no-track -b migrate/$REPO_NAME origin/main
git merge --allow-unrelated-histories migration/$REPO_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment