Copying a component from one monorepo to another

Posted by Ilya on January 03, 2022

Concept of monorepo

When developing frontend application, it can be useful to store custom components in the same repo as the application itself. Or whole repo can be a collection of the components.

This post describing situation when you need to reuse and change a component from the other monorepo.

Step by step guide

Extracting a component

First we need a copy of the source repo to extract component from, in it we will change the repo's root to the component's subdirectory.


git checkout -b my_branch v2.0.0
export FILTER_BRANCH_SQUELCH_WARNING=1
git filter-branch --prune-empty --subdirectory-filter path/to/component my_branch -- --all


It's important to have a branch. When executing filter-branch on detached head it works inadequately.

Adding a new component to the target repo

In the target repo, execute

git subtree add -P path/to/component ../path/to/source/repo my_branch

Voila!



tech frontend git

Leave a comment on how you felt of the article...

Author Name
Author Email
Note: This email will not be shared to anyone
Comment