Is there a way to "refresh" an imported repository with Lerna? Is there a way to "refresh" an imported repository with Lerna? git git

Is there a way to "refresh" an imported repository with Lerna?


Using the answer by @Doğancan Arabacı and comment by @Matt Mazzola. I was able to do this for myself, but I've added my own answer with a few more details to try and give a clearer explanation.

I also faced this problem as lerna import will only allow you to import once if the directory exists you can’t import. See code here.

The lerna import command takes all the commits from your original repository, reverses and replays them. However there’s no way to replay these from when branches diverge (as you might with the git rebase --onto command). See here I get the feeling that you could possibly achieve it using git rebase or using a similar technique to pick out where branches diverge to extend the lerna import command. I also get the feeling that might get messy or take while so the simple way that currently exists is:

For each branch you wish to import:

From the original repo (referred to as original:

  • Checkout and pull the branch you wish to import
  • Cut a new branch from the branch you wish to import: git checkout -b lerna-export
  • Move everything into a directory where it will be imported to e.g. packages/original something like: mkdir packages && mkdir packages/original
  • Move all you files inside the new directory: git mv -k * ./packages/original - you may have to copy over any files that aren’t picked

Then from the Lerna repo:

  • Add the original repo as a remote git remote add original ###url of repo###
  • Switch to the branch you want to import to git checkout -b orignal-import
  • Merge the branch from original to lerna: git merge original/lerna-export --allow-unrelated-histories
  • Fix any conflicts if you have any
  • Push to the Lerna branch git push

After all branches imported, you may want to remove the second remote once all the branches are imported: git remove rm original

I had some issues with the security on our BitBucket instance as I was pushing commits by other authors so I had to rewrite the git history with git filter-branch, but that doesn’t seem totally related to the question to provide details of it.


I'm not sure what lerna is doing underhood but there is manual way of doing it with git. We did similar thing in past for 8-10 repositories.

Let's assume we have MonoRepo and TargetRepo

  1. Go to MonoRepo
  2. git remote add target
  3. git checkout -b feature1
  4. git merge target/feature1-branch-on-target
  5. repeat steps 3 and 4 for all desired branches.
  6. profit

You can repeat steps 3-4 whenever you want, after a few commits, do everything at one day and move to mono repo etc.