git:符号链接/引用外部存储库中的文件 [英] git: symlink/reference to a file in an external repository

查看:180
本文介绍了git:符号链接/引用外部存储库中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能在git中有一个链接到git仓库中的特定文件吗?就像git子模块为文件夹做的一样,但我的问题是关于一个特定的文件,而不是一个完整的目录:

  my-project / 
class1.java
class2.java
logback.xml(链接到特定文件,说https://github.com/theHilikus/JRoboCom/blob/master/jrobocom-core/src/ main / resources / logback.xml)

可以看出,在这种情况下,它没有有意义的是有一个完整的子模块文件夹,它只是一个文件。

我确定链接被锁定到一个特定的提交,但它会更好,如果它正在移动,因为它改变了自己的项目生命周期



注意,这与文件系统的符号链接无关;我在谈论在另一个项目,回购,分支或任何文件中的引用。如果该文件的内容是重复的而不是文件系统符号链接,那么就可以了。

解决方案

Git具有您可以使用的功能实现你所需要的。它支持文件系统符号链接并支持子模块。子模块已经是处理对其他存储库的引用的标准方式。您可以将它们与本地引用文件的方式结合使用。这可以直接使用相对符号链接处理,也可以间接使用将子模块中的文件复制到需要它们的位置的脚本。



每个外部git应该有一个子模块树,你应该小心对待子模块,因为它们不仅是外部存储库的链接,而且也是其特定的提交。以下解决方案将向您展示如何使用来自外部存储的单个文件,但仍保留子模块的所有优点。

另一种方法是直接获取文件,但那么你将完全丧失子模块的优势,或者你将不得不自己构建这些功能。正如我已经说过的,子模块是处理这类任务的标准方式,除非您有特殊需求,例如避免不惜一切代价下载其他文件。



使用子模块和符号链接

准备好子模块后,可以添加指向子模块目录结构的文件系统符号链接。 / p>

在您的项目目录的shell中运行:

  $ git子模块添加https://github.com/theHilikus/JRoboCom 
$ ln -s JRoboCom / jrobocom-core / src / main / resources / logback.xml
$ git add .gitmodules logback.xml
$ git commit -m给logback.xml添加一个符号链接,其中包含相应的子模块

现在你有一个符号链接:

  logback.xml  - > JRoboCom / jrobocom-core / src / main / resources / logback.xml 



使用子模块和脚本



或者,您可以使用自定义脚本来复制子模块中的普通文件。在非常特殊的情况下,您可以通过脚本处理外部存储库而无需使用子模块,但我通常不会推荐它。



创建一个文件 bootstrap.sh 包含:

 #!/ bin / sh 
git submodule init
git子模块更新

cp JRoboCom / jrobocom-core / src / main / resources / logback.xml。

在项目目录的shell中运行:

  $ git submodule add https://github.com/theHilikus/JRoboCom 
$ git add .gitmodules bootstrap.sh
$ git commit -m添加一个脚本以从相应的子模块

中获取logback.xml请注意,我们不添加 logback.xml file到Git,因为它将从子模块获取。



指示存储库的用户首先运行上面的脚本。它将准备使用子模块的存储库,将获取子模块数据并将文件复制到其位置。有时在项目中已经有了某种引导脚本。



使用脚本通过git协议获取单个文件



使用 git archive 为Git> = 1.7.9.5找到另一个解决方案。创建文件引导程序。 sh包含:

 #!/ bin / sh 
git archive --remote = https://github.com / theHilikus / JRoboCom master:JRoboCom / jrobocom-core / src / main / resources logback.xml | tar -x

在项目目录的shell中运行:

  $ git add bootstrap.sh 
$ git commit -m添加脚本以直接从远程项目获取logback.xml



使用脚本通过HTTP获取单个文件



如果存储库托管服务也通过HTTP为单个文件提供服务,则可以简单地使用 curl wget 来下载它们。



创建一个文件bootstrap.sh,其中包含:

 #!/ bin / sh 
curl -O https://raw.githubusercontent.com/theHilikus/JRoboCom/master/jrobocom-core/src/main/resources/logback.xml

在您的项目目录的shell中运行:

  $ git add bootstrap.sh 
$ git commit -m添加一个脚本直接从github获取logback.xml



获取单个文件的脚本说明



您也可以将引用存储在具有特定扩展名的文件中(例如 *。url )或将引用列表保存在一个文件中(如 .references 在你的项目目录中),并构建一个更全面的脚本,通过所有的参考和下载各自的文件。


Is it possible in git to have a "link" to a particular file in a git repo? Like what git submodules do for folders but my question is about a particular file, not a full directory:

my-project/
    class1.java
    class2.java
    logback.xml (link to a particular file, say https://github.com/theHilikus/JRoboCom/blob/master/jrobocom-core/src/main/resources/logback.xml)

So as can be seen, in this case it doesn't make sense to have a whole submodule folder, it is just one file.
I'm ok with the link being locked to a particular commit but it would be better if it's moving as it changes in its own project's lifecycle

As a note, this has nothing to do with file-system's symbolic links; I'm talking about a reference to a file in another project, repo, branch, or anything. it's ok if the content of the file is a duplicate and not a file-system symlink

解决方案

Git has features that you can use to achieve what you need. It supports file system symlinks and it supports submodules. Submodules is already a standard way to handle references to other repositories. You can use them in conjunction with a way to reference files locally. That can be handled directly using relative symbolic links or indirectly using a script that copies over files from the submodule to where you need them.

You should have one submodule per external git tree and you should treat the submodules with care, as they are not only links to external repositories but also to their specific commits. The following to solutions will show you how to use individual files from an external repostiory but still maintain all the advantages of submodules.

An alternative way is to fetch the files directly but then you will lose the advantage of submodules entirely or you will have to build the features yourself. As I already stated, submodules are the standard way to handle this sort of task and you should use it unless you have special needs like to avoid downloading other files at all cost.

Using a submodule and a symlink

Once you have a submodule ready, you can just add filesystem symlinks pointing into the submodule directory structure.

Run this in a shell in your project directory:

$ git submodule add https://github.com/theHilikus/JRoboCom
$ ln -s JRoboCom/jrobocom-core/src/main/resources/logback.xml
$ git add .gitmodules logback.xml
$ git commit -m "add a symbolic link to logback.xml with the respective submodule"

Now you have a symlink:

logback.xml -> JRoboCom/jrobocom-core/src/main/resources/logback.xml

Using a submodule and a script

As an alternative, you can use custom scripts that copy over ordinary files from your submodules. In very special cases you could handle the external repositories from the script without submodules but I would normally not recommend it.

Create a file bootstrap.sh containing:

#!/bin/sh
git submodule init
git submodule update

cp JRoboCom/jrobocom-core/src/main/resources/logback.xml .

Run this in a shell in your project directory:

$ git submodule add https://github.com/theHilikus/JRoboCom
$ git add .gitmodules bootstrap.sh
$ git commit -m "add a script to fetch logback.xml from the respective submodule"

Note that we are not adding the logback.xml file to Git, as it will be fetched from the submodule.

Instruct users of the repository to first run the script above. It will prepare their repositories for using submodules, will fetch the submodule data and will copy the file to its location. Sometimes there's already some sort of bootstrap script in the project.

Using a script to fetch a single file via git protocol

Found another solution for Git >= 1.7.9.5 using git archive.

Create a file bootstrap.sh containing:

#!/bin/sh
git archive --remote=https://github.com/theHilikus/JRoboCom master:JRoboCom/jrobocom-core/src/main/resources logback.xml | tar -x

Run this in a shell in your project directory:

$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from the remote project"

Using a script to fetch a single file via HTTP

If the repository hosting service also serves the individual files via HTTP, you can simply use curl or wget to download them.

Create a file bootstrap.sh containing:

#!/bin/sh
curl -O https://raw.githubusercontent.com/theHilikus/JRoboCom/master/jrobocom-core/src/main/resources/logback.xml

Run this in a shell in your project directory:

$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from github"

Notes on scripts fetching single files

You could also store the references in files with a specific extention (like *.url) or maintain the list of references in one file (like .references in your project directory) and build a more comprehensive script that goes through all the references and downloads the respective files.

这篇关于git:符号链接/引用外部存储库中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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