This is a guide on how you can move a repository into an existing monorepo while keeping the entire history.
First, let's define some variables:
REPO_ORIGIN=github.com
REPO_USER=idleberg
REPO_NAME=demoWithin 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_NAMELet'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
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
doneYou 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)
You can now commit all the changes
git checkout --no-track -b migrate/$REPO_NAME origin/main
git merge --allow-unrelated-histories migration/$REPO_NAME