Merge Git Repository into Monorepo
From a repository
- Clone repository to a new directory
git clone <repo-url> <package>
- 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
- Add remote in monorepo to package
git remote add <package> /path/to/<package>
- Fetch commits from package
git fetch <package> --no-tags
--no-tags - do not fetch tags to avoid polluting monorepo tags
- Merge
git merge <package>/master --allow-unrelated-histories
--allow-unrelated-histories - override Git restriction to merge branch that\ doesn't have a common ancestor.
- Remove remote
git remote remove <package>