How to fork part of a monorepo and still being abl

2019-07-26 04:10发布

问题:

Sometimes I find monorepo on GitHub that consists of multiple npm packages, which I would like to make some modification and use it in my projects. But it's much harder for npm to install package from a git subdirectory than to install from a git repo (at least for now, until the linked issue is resolved). Since I would make my own modification, I wonder how can I set up my own git repository so that it is easy for npm to install, and for me to merge upstream changes.

Currently, I used this guide from GitHub to split the package from the rest of the monorepo, i.e.

git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME  BRANCH-NAME 

Npm can install the repo easily, but I find it difficult to merge any upstream change.

Has anyone done this before? Any idea?

回答1:

Actually, I think I missed something - merging upstream changes is not as hard as I thought, because git will (magically) produce the same SHA1 for the same subtree split, even if you repeat the process a second time.


So here's my solution:

  1. Clone the monorepo
  2. Rename the default branch (master) to something else e.g. upstream
  3. Split the subdirectory from the monorepo

    git checkout upstream
    git subtree split -P <subdirectory path> -b master
    

    Now we have a master branch sitting there with only commits related to the subdirectory path.

  4. (optional) Set the remote for master branch and push it to GitHub.

Now if the upstream monorepo added some changes:

  1. Checkout the upstream branch and pull the changes

    git checkout upstream
    git pull
    
  2. Split again

    git subtree split -P <subdirectory path> -b upstream-patch
    

    Check the revision graph, you will see that the new branch (upstream-patch) is related to master.

    gitk master upstream-patch
    
  3. Now simply merge the new branch to master

    git checkout master
    git merge upstream-patch
    

    Manually resolve any merge conflicts.

  4. (optional) git push


回答2:

That's not possible to fork part of a monorepository because git won't be able to calculate the good Sha1 to be able to sync with upstream.

But perhaps that "sparse checkout" is what you are looking for. That let you choose the subdirectories you want in your working directory.

See for example https://gist.github.com/sumardi/5559896 and http://schacon.github.io/git/git-read-tree.html#_sparse_checkout



标签: git github npm