5

I have a monorepo with yarn workspaces (and lerna)

-package
--one
--two
-site

And a script that takes each one of them (one, two and site) and pushes them to their github repositories

I do this with something like

git subtree split ... // from `one|two|site` folders create a new repo
git checkout ... // switch to the new repo
git tag ... // create a tag and push it to the new repo
git push -u url branch:master // push the content to the new repo
git checkout master // go back
git branch --delete // delete the created brancg

It (so far) works, even though it is slow

The question is whether I am doing this correctly or there is any other better solution out there

GWorking
  • 4,011
  • 10
  • 49
  • 90
  • 1
    Did you consider submodules? – VonC Oct 06 '19 at 05:00
  • Let me confirm my understanding of your structure. The monorepo is in fact read/write git repo, where all the work is done. While "one", "two", "three" are readonly git repos presumably for distribution. Is this right? – sbat Oct 06 '19 at 18:29
  • @sbat yes, that's right – GWorking Oct 06 '19 at 21:19
  • 1
    @VonC do they work better than subtree? I remember I looked into that but I decided to go with subtree (don't remember why though) – GWorking Oct 06 '19 at 21:20
  • 1
    I find them easier to manipulate. And tagging them all in one go is possible, as I proposed here: https://stackoverflow.com/a/58254305/6309 – VonC Oct 06 '19 at 21:24
  • It looks like Lerna leave this to the end user, but unofficially blesses https://github.com/splitsh/lite . See the discussion here https://github.com/lerna/lerna#git-hosted-dependencies. – sbat Oct 07 '19 at 07:51

1 Answers1

1

The initial subtree split seems OK, but you might want to double-check example 3.

Suppose you have a source directory with many files and subdirectories, and you want to extract the lib directory to its own git project. Here's a short way to do it:

First, make the new repository wherever you want:

$ <go to the new location>    
$ git init --bare

Back in your original directory:

$ git subtree split --prefix=lib --annotate="(split)" -b split

Then push the new branch onto the new empty repository:

$ git push <new-repo> split:master

https://github.com/git/git/blob/master/contrib/subtree/git-subtree.txt

Anyway, following that, if this monorepo is the "write" repository, you should be able to simply subtree push updates to the remotes.

git subtree push --prefix package/one remote-one master

There is some fuss about pushes taking longer and longer (the more commits the repository collects), so there's some info here about using the rejoin option with your split.

How can I reduce the ever-increasing time to push a subtree?

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188