Git - 包含来自其他存储库的文件 [英] Git - Include files from other repositories
问题描述
一个有点平行的例子:在ASP.NET中,我会做这通过引用其他库的 .dll
文件(不带copy local),以便每当我重新编译外部我的项目已经指向最新版本。
示例场景
我的项目是... MyProject ,而我只想从外部存储库 ExternalLibrary 和 External2 中创建几个文件。我不想将每个存储库克隆到其他地方,并将这些文件复制粘贴到我的存储库中。如果我对 MyProject / scripts / common1.js
进行了改进,我想将它推回到 ExternalLibrary 。
- MyProject /
- index.html
- 脚本/
- mycode.js
- otherplugin .js
- common1.js < - 来自ExternalLibrary
- plugin2.js < - 来自ExternalLibrary
- 款式/
- mystyle.css
- common.css < - 来自External2
- ExternalLibrary /
$ b- common1。
- plugin1.js
- plugin2.js
- bunchofothercrap ...
- common.css >
- bunchofothercrap ...
相关参考erences:
- / checkout-only-file-from-git>如何仅从git存储库签出一个文件?
- >
- 可以创建一个git仓库,其分支是来自其他仓库的克隆吗?
- http://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html
Git不支持挑选远程项目中的选择文件。你可以选择你想要的子树文件,然后找出某种策略让它恢复,但这通常是复杂的,特别是如果你突然决定你想要的文件多于你最初选择的文件。 p>
我通常最终做的是使用 symlinks
。这可以通过将项目添加为项目中的子模块,然后将文件符号链接到正确的位置来完成。
您的示例将如下所示:
$ git clone< url> myproject
$ cd myproject
$ git submodule add< url> external_library
$ git submodule add< url> external2
$ cd脚本
$ ln -s ../external_library/common1.js
$ ln -s ../external_library/plugin2.js
$ cd ../styles
$ ln -s ../external2/common.css
要使符号链接正常工作在Windows中:
- 如果您是管理员组的成员,则必须关闭用户访问控制(UAC),然后重新启动计算机。
- 如果您不是管理员,您需要获取 SeCreateSymbolicLinkPrivilege权限
然后在进行任何更改之前,我会去创建一个新的分支:
$ cd external_library
$ git checkout -b myproject
#使变更回到../myproject
$ git commit< files> #in external_library
然后,您可以将分支推送到github或其他东西,然后向作者提交拉取请求您可以保持同步,如下所示:
$ cd external_library
$ git checkout master
$ git pull
$ git checkout myproject
$ git merge master
您还需要在子模块更改时更新父项目(例如 git commit external_library
) 。任何使用你项目的人都需要做一个 git clone --recursive< url_to_myproject>
。
$ b 可以在不使用项目的独立分支的情况下实现其中的大部分功能。使用对你最有意义的事情。
With Git, I'd like to include some common JS/CSS library and/or utility methods (i.e. specific files from another repo) in my project, and I'd like it so they're always up-to-date. I don't really want the entire remote repository(s). Bonus if I can work on my "local copies" of the remote files and push the changes back up.
A somewhat parallel example: in ASP.NET, I would do this by referencing the ("remote") other libraries' .dll
files (without "copy local"), so that whenever I recompile the external libraries my project would already point to the newest versions.
Example Scenario
My project is...MyProject, and I just want a couple files from external repositories ExternalLibrary and External2. I'd prefer not to clone each repository somewhere else and copy-paste the files into my repo. If I make an improvement to MyProject/scripts/common1.js
, I'd like to push that back to ExternalLibrary.
- MyProject/
- index.html
- scripts/
- mycode.js
- otherplugin.js
- common1.js <-- from ExternalLibrary
- plugin2.js <-- from ExternalLibrary
- styles/
- mystyle.css
- common.css <-- from External2
- ExternalLibrary/
- common1.js
- plugin1.js
- plugin2.js
- bunchofothercrap...
- External2/
- common.css
- bunchofothercrap...
Related references:
- Git: how to work with multiple repositories?
- How to checkout only one file from git repository?
- Get file from other developer local GIT and add to main repository
- Can a git repository be created where branches are clones from other repositories?
- http://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html
Git does not have any support for picking out select files in a remote project. You could pick out the files you want with subtree and then figure out some sort of strategy for getting it back in, but that usually is way to complicated, especially if you suddenly decide you want a couple more files than you originally picked.
What I usually end up doing is using symlinks
. This can be done by adding the projects as submodules in your project, then symlinking the files into the right location.
Your example would go something like this:
$ git clone <url> myproject
$ cd myproject
$ git submodule add <url> external_library
$ git submodule add <url> external2
$ cd scripts
$ ln -s ../external_library/common1.js
$ ln -s ../external_library/plugin2.js
$ cd ../styles
$ ln -s ../external2/common.css
To get symlinks to work in windows:
- If you are a member of the Administrators group you have to turn off User Access Control (UAC) and then restart the computer.
- If you are not an administrator you need to get the SeCreateSymbolicLinkPrivilege privilege
Then before you make any changes I would go and create a new branch:
$ cd external_library
$ git checkout -b myproject
# make changes back in ../myproject
$ git commit <files> # in external_library
Then you can push your branch to github or something and submit a pull request to the author in order to contribute your changes back.
You can stay up to sync like this:
$ cd external_library
$ git checkout master
$ git pull
$ git checkout myproject
$ git merge master
You also need to update the parent project when the submodule changes (e.g. git commit external_library
). Anyone using your project would need to do a git clone --recursive <url_to_myproject>
.
NOTE: You can achieve most of this without using a separate branch for your project. Use whatever makes most sense to you.
这篇关于Git - 包含来自其他存储库的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!