Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Last active April 25, 2019 18:58
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JamieMason/b6e1401bd79e66274c768d1fe49d5bed to your computer and use it in GitHub Desktop.
Save JamieMason/b6e1401bd79e66274c768d1fe49d5bed to your computer and use it in GitHub Desktop.
Bash script to migrate multiple projects into one Lerna monorepo (https://lernajs.io)
#!/usr/bin/env bash
set -x
shopt -s extglob dotglob
cd "$HOME"
rm -rf "$HOME/TEMP_DIR"
mkdir "$HOME/TEMP_DIR"
cd "$HOME/TEMP_DIR"
git init
function clone_repo () {
git clone "https://github.com/some-username/$1.git"
cd "$1"
mkdir -p "packages/$1"
git mv !(packages|.git|..|.) packages
cd packages
git mv !("$1") "$1"
cd "$HOME/TEMP_DIR/$1"
git add . -A
git commit -m "Move source to packages/$1" -n
cd "$HOME/TEMP_DIR"
}
clone_repo "some-package-1"
clone_repo "some-package-2"
clone_repo "some-package-3"
cd "$HOME/TEMP_DIR"
mkdir lerna-monorepo
cd lerna-monorepo
git init
git pull $HOME/TEMP_DIR/some-package-1 master --allow-unrelated-histories -s resolve
git pull $HOME/TEMP_DIR/some-package-2 master --allow-unrelated-histories -s resolve
git pull $HOME/TEMP_DIR/some-package-3 master --allow-unrelated-histories -s resolve
set +x
shopt -u extglob dotglob
@JamieMason
Copy link
Author

JamieMason commented Aug 17, 2017

Migrate multiple projects into one Lerna Monorepo

It's easy to run into conflicts because files which likely appear in all repos (such as package.json) can result in conflicts. Git sees those files moved from the root to packages/ many times and doesn't know what you want to do.

So you first need to move the source down into packages/ and commit it in each of the imported repos then git pull them into the new monorepo afterwards.

The merge strategy of resolve is important to help avoid conflicts, however if you see the error warning: no common commits then try again without it.

When you've run this script, run git remote add https://github.com/username/repo.git so you can git push it.

Any questions get me at https://twitter.com/fold_left as Gists don't notify you when someone comments on a Gist.

@pmaoui
Copy link

pmaoui commented Oct 8, 2018

Thank you for your script. It didn't work out of the box for me... I had to remove master for the git pull part:

git pull $HOME/TEMP_DIR/some-package-1 master --allow-unrelated-histories -s resolve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment