Git - 包含来自其他存储库的文件 [英] Git - Include files from other repositories

查看:142
本文介绍了Git - 包含来自其他存储库的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Git,我想在我的项目中包含一些常见的JS / CSS库和/或实用程序方法(即来自另一个回购的特定文件),并且我喜欢它总是最新的。我并不想要整个远程存储库。如果我可以在远程文件的本地副本上工作,并重新推送更改,则可获得额外奖励。



一个有点平行的例子:在ASP.NET中,我会做这通过引用其他库的 .dll 文件(不带copy local),以便每当我重新编译外部我的项目已经指向最新版本。



示例场景



我的项目是... MyProject ,而我只想从外部存储库 ExternalLibrary External2 中创建几个文件。我不想将每个存储库克隆到其他地方,并将这些文件复制粘贴到我的存储库中。如果我对 MyProject / scripts / common1.js 进行了改进,我想将它推回到 ExternalLibrary


  1. MyProject /


    • index.html

    • 脚本/


      • mycode.js

      • otherplugin .js

      • common1.js < - 来自ExternalLibrary

      • plugin2.js < - 来自ExternalLibrary


    • 款式/


      • mystyle.css

      • common.css < - 来自External2



  2. ExternalLibrary /
    $ b

    • common1。

    • plugin1.js

    • plugin2.js

    • bunchofothercrap ...


    • common.css
    • >
    • bunchofothercrap ...


相关参考erences:




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中:





然后在进行任何更改之前,我会去创建一个新的分支:

  $ 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.

  1. MyProject/
    • index.html
    • scripts/
      • mycode.js
      • otherplugin.js
      • common1.js <-- from ExternalLibrary
      • plugin2.js <-- from ExternalLibrary
    • styles/
      • mystyle.css
      • common.css <-- from External2
  2. ExternalLibrary/
    • common1.js
    • plugin1.js
    • plugin2.js
    • bunchofothercrap...
  3. External2/
    • common.css
    • bunchofothercrap...

Related references:

解决方案

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:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆