Skip to content

Instantly share code, notes, and snippets.

@sjmeverett
Last active June 1, 2022 18:05
Show Gist options
  • Save sjmeverett/8b8fe166eeaca24d43d1ae092ae7a168 to your computer and use it in GitHub Desktop.
Save sjmeverett/8b8fe166eeaca24d43d1ae092ae7a168 to your computer and use it in GitHub Desktop.
Script to import a repository into a monorepository while maintaining git history
#!/bin/bash
tempdir=/tmp/mono-import
monorepo=$(pwd)
package=$(basename -s .git $1)
# fail on errors
set -e
# make sure we're in a git repo
git status || exit 1
# make sure the working dir is available
mkdir -p $tempdir
# clone the repo to import
cd $tempdir
rm -rf $package
git clone $1
# move the package source to the correct target directory, but inside the package itself
cd $package
mkdir -p packages/$package
ls -A | grep -v -e ^packages -e ^.git$ | xargs -I{} git mv {} packages/$package
# commit the result
git commit -m "Move ${package} for monorepo"
# now go back to the monorepo dir
cd $monorepo
# add our temp clone as a remote
remote=monorepo-import-$package
git remote add $remote $tempdir/$package
git fetch $remote
# merge it into the monorepo
branch=$(git remote show $remote | grep "HEAD branch" | sed 's/.*: //')
git merge $remote/$branch --allow-unrelated-histories --no-edit
git remote rm $remote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment