Skip to main content

Merge Git Repository into Monorepo

From a repository

  1. Clone repository to a new directory
git clone <repo-url> <package>
  1. Filter list of commits
export TARGET_DIR="packages/<package>"

git filter-branch --prune-empty --tree-filter '
mkdir -p "$TARGET_DIR"
git ls-tree --name-only $GIT_COMMIT | xargs -I files mv files "$TARGET_DIR"
'

The --prune-empty - removes commits which are empty due to the rewrite.

From a monorepo

  1. Add remote in monorepo to package
git remote add <package> /path/to/<package>
  1. Fetch commits from package
git fetch <package> --no-tags

--no-tags - do not fetch tags to avoid polluting monorepo tags

  1. Merge
git merge <package>/master --allow-unrelated-histories

--allow-unrelated-histories - override Git restriction to merge branch that\ doesn't have a common ancestor.

  1. Remove remote
git remote remove <package>