Moving to monorepos without losing history

November 17, 2015

If you’d like to merge two (or more) git repositories together and preserve the commit histories of both, here’s the script for you:

cd some-repo

git remote add other-repo
git fetch other-repo
git checkout other-repo master

git checkout -b merge-other-repo
mkdir other-repo

for f in *; do
  git mv $f other-repo

# If you're making a merge request:

git merge master

git push origin merge-other-repo

# Then make a merge request

# If you're pushing directly to master:

git checkout master

git merge merge-other-repo

git push origin master