git 来自 github repo 子文件夹的 npm 安装包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39194648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
npm install package from github repo subfolder
提问by Ceridan
Is it possible to install npm package from github when the package located inside subfolder?
当包位于子文件夹中时,是否可以从 github 安装 npm 包?
For example, we have Microsoft BotBuilder repository: https://github.com/Microsoft/BotBuilder
例如,我们有 Microsoft BotBuilder 存储库:https: //github.com/Microsoft/BotBuilder
But I need to install package inside subfolder "Node/core/": https://github.com/Microsoft/BotBuilder/tree/master/Node/core/
但我需要在子文件夹“Node/core/”中安装包:https: //github.com/Microsoft/BotBuilder/tree/master/Node/core/
So how can I install it with npm?
那么如何使用 npm 安装它呢?
采纳答案by Tomasz Jakub Rup
Add to package.json
:
添加到package.json
:
...
"scripts": {
"postinstall": "mkdir BotBuilder; cd BotBuilder; git init; git remote add -f origin https://github.com/Microsoft/BotBuilder.git; git config core.sparseCheckout true; echo \"Node/core\" >> .git/info/sparse-checkout; git pull --depth=1 origin master; cd ..; npm i ./BotBuilder/Node/core/"
...
},
...
postinstall
script is running after the package is installed.
postinstall
安装包后运行脚本。
And step by step:
并一步一步:
- Make folder to clone repo:
mkdir BotBuilder
- enter to the folder:
cd BotBuilder
- init git repo:
git init
- set git origin to Microsoft/BotBuilder repo:
git remote add -f origin https://github.com/Microsoft/BotBuilder.git
- enable sparse checkout:
git config core.sparseCheckout true
- add
Node/core
to checkout list:echo "Node/core" >> .git/info/sparse-checkout
- pull part of repo:
git pull --depth=1 origin master
- enter to Your app folder:
cd ..
- install BotBuilder:
npm i ./BotBuilder/Node/core/
- 创建文件夹以克隆 repo:
mkdir BotBuilder
- 进入文件夹:
cd BotBuilder
- 初始化git仓库:
git init
- 将 git origin 设置为 Microsoft/BotBuilder 存储库:
git remote add -f origin https://github.com/Microsoft/BotBuilder.git
- 启用稀疏结帐:
git config core.sparseCheckout true
- 添加
Node/core
到结帐列表:echo "Node/core" >> .git/info/sparse-checkout
- 拉一部分回购:
git pull --depth=1 origin master
- 进入您的应用程序文件夹:
cd ..
- 安装机器人生成器:
npm i ./BotBuilder/Node/core/
回答by Thembelani M
Might be slightly off topic, just still relevant to the question
可能有点偏离主题,只是仍然与问题相关
https://git-scm.com/book/en/v2/Git-Tools-Submodules
https://git-scm.com/book/en/v2/Git-Tools-Submodules
Git Submodules are git repos that you can use in other repos (henceforth, referred to as Supermodules). With each submodule having the usual assortment of branches features and tags, the benefit comes from each supermodule being a version controlled, pluggable components, that can be worked on separately or developed alongside the supermodule.
Git 子模块是 git 存储库,您可以在其他存储库中使用它们(以下称为超级模块)。由于每个子模块都具有通常的各种分支特性和标签,每个超级模块都是一个版本控制、可插拔的组件,可以单独工作或与超级模块一起开发。
A Few Useful Commands
一些有用的命令
To add a submodule, you run the following inside your supermodule:
要添加子模块,请在超级模块中运行以下命令:
git submodule add <url-to-submodule-repo>
The submodule(s) still have to be initialized and fetched from the repo:
子模块仍然需要初始化并从 repo 中获取:
git submodule init
git submodule update
git submodule init
git submodule update
A supermodule with submodules can be cloned and all submodules fetched by running:
可以克隆带有子模块的超级模块并通过运行获取所有子模块:
git clone --recursive <url-to-supermodule>
git clone --recursive <url-to-supermodule>
You can pull upstream changes to a submodule's branch by running the following inside the submodule directory:
您可以通过在子模块目录中运行以下命令来将上游更改拉到子模块的分支:
git fetch
git fetch
Then run the following to update local code:
然后运行以下命令来更新本地代码:
git merge
git merge
The following will fetch and merge for all submodules in your supermodule:
以下将获取并合并超级模块中的所有子模块:
git submodule update --remote
git submodule update --remote
If you want to track a specific branch of a submodule you can use the following:
如果要跟踪子模块的特定分支,可以使用以下命令:
git config -f .gitmodules submodule.<my-submodule>.branch fantastic_new_implementation
git config -f .gitmodules submodule.<my-submodule>.branch fantastic_new_implementation
If you have worked on your supermodules and submodules and you push your supermodule, changes made to submodules will only exist locally and those you are collaborating with will not know of these changes. The following command will check if your submodules have been pushed BEFORE attempting to push your supermodule
如果你已经在你的超级模块和子模块上工作并且你推送你的超级模块,对子模块所做的更改将只存在于本地,而与你合作的人不会知道这些更改。以下命令将在尝试推送您的超级模块之前检查您的子模块是否已被推送
git push --recurse-submodules=check
git push --recurse-submodules=check
Finally, here is a useful ForEachcommand, that allows us to run a command for each submodule
最后,这是一个有用的ForEach命令,它允许我们为每个子模块运行一个命令
git submodule foreach 'git checkout -b featureA
git submodule foreach 'git checkout -b featureA