Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
git:外部存储库中文件的符号链接/引用_Git - Fatal编程技术网

git:外部存储库中文件的符号链接/引用

git:外部存储库中文件的符号链接/引用,git,Git,在git中,是否有可能在git回购中拥有指向特定文件的“链接”?就像git子模块对文件夹所做的一样,但我的问题是关于特定文件,而不是完整目录: 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/lo

在git中,是否有可能在git回购中拥有指向特定文件的“链接”?就像git子模块对文件夹所做的一样,但我的问题是关于特定文件,而不是完整目录:

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)
$ 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"
$ 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"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from the remote project"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from github"
可以看出,在这种情况下,拥有整个子模块文件夹是没有意义的,它只是一个文件。
我同意将链接锁定到一个特定的提交,但如果它在自己的项目生命周期中发生变化时移动会更好


请注意,这与文件系统的符号链接无关;我说的是对另一个项目、回购、分支或其他任何项目中的文件的引用。如果文件的内容是重复的,而不是文件系统符号链接,则没有问题。不幸的是,没有。git中唯一可以使用的外部引用类型是子模块,它将整个分支放在一个目录中。您需要创建一个脚本或其他东西来帮助从所需位置获取文件并将其放入工作树中。

公认的答案似乎有误导性,因为只要所有开发人员使用的操作系统都支持符号链接,Git就可以很好地处理符号链接

Git在默认情况下尝试存储符号链接,而不是遵循它们(为了紧凑性和人们通常想要的东西)

签出包含链接的树时,无论目标文件系统对象是否存在,它都会将对象还原为符号链接

如果您所处的文件系统类似FAT,不支持符号链接,并且您的存储库使用它们,则可以将core.symlinks配置变量设置为false,并且符号链接将作为包含链接文本的小纯文本文件检出

因此,参考资料:


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

每个外部git树应该有一个子模块,并且应该谨慎对待这些子模块,因为它们不仅是指向外部存储库的链接,而且是指向特定提交的链接。以下解决方案将向您展示如何使用外部报告中的单个文件,但仍保持子模块的所有优势

另一种方法是直接获取文件,但这样会完全失去子模块的优势,或者必须自己构建功能。正如我已经说过的,子模块是处理这类任务的标准方法,除非您有特殊需要,比如不惜一切代价避免下载其他文件,否则您应该使用它

使用子模块和符号链接 一旦子模块就绪,就可以添加指向子模块目录结构的文件系统符号链接

在项目目录中的shell中运行此命令:

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)
$ 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"
$ 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"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from the remote project"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from github"
现在您有了一个符号链接:

logback.xml -> JRoboCom/jrobocom-core/src/main/resources/logback.xml
使用子模块和脚本 作为替代方案,您可以使用自定义脚本复制子模块中的普通文件。在非常特殊的情况下,您可以从脚本处理外部存储库,而不需要子模块,但我通常不推荐这样做

创建包含以下内容的文件
bootstrap.sh

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

cp JRoboCom/jrobocom-core/src/main/resources/logback.xml .
#!/bin/sh
git archive --remote=https://github.com/theHilikus/JRoboCom master:JRoboCom/jrobocom-core/src/main/resources logback.xml | tar -x
#!/bin/sh
curl -O https://raw.githubusercontent.com/theHilikus/JRoboCom/master/jrobocom-core/src/main/resources/logback.xml
在项目目录中的shell中运行此命令:

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)
$ 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"
$ 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"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from the remote project"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from github"
请注意,我们没有将
logback.xml
文件添加到Git,因为它将从子模块获取

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

使用脚本通过git协议获取单个文件 使用
Git-archive
为Git>=1.7.9.5找到了另一个解决方案

创建包含以下内容的bootstrap.sh文件:

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

cp JRoboCom/jrobocom-core/src/main/resources/logback.xml .
#!/bin/sh
git archive --remote=https://github.com/theHilikus/JRoboCom master:JRoboCom/jrobocom-core/src/main/resources logback.xml | tar -x
#!/bin/sh
curl -O https://raw.githubusercontent.com/theHilikus/JRoboCom/master/jrobocom-core/src/main/resources/logback.xml
在项目目录中的shell中运行此命令:

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)
$ 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"
$ 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"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from the remote project"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from github"
使用脚本通过HTTP获取单个文件 如果存储库托管服务也通过HTTP为单个文件提供服务,那么您可以简单地使用
curl
wget
下载它们

创建包含以下内容的bootstrap.sh文件:

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

cp JRoboCom/jrobocom-core/src/main/resources/logback.xml .
#!/bin/sh
git archive --remote=https://github.com/theHilikus/JRoboCom master:JRoboCom/jrobocom-core/src/main/resources logback.xml | tar -x
#!/bin/sh
curl -O https://raw.githubusercontent.com/theHilikus/JRoboCom/master/jrobocom-core/src/main/resources/logback.xml
在项目目录中的shell中运行此命令:

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)
$ 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"
$ 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"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from the remote project"
$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from github"
关于脚本获取单个文件的说明
您还可以将引用存储在具有特定扩展名的文件中(如
*.url
),或在一个文件中维护引用列表(如项目目录中的
.references
)并构建一个更全面的脚本,它将遍历所有引用并下载相应的文件。

我不知道git中有什么方法。。。但是不是让git管理文件,而是让您的构建系统(maven?)在需要时下载文件?参考:子模块的可能重复是处理外部引用的标准方法。您可以将它们与文件系统符号链接或脚本一起使用,请参阅我的答案。@PavelŠimerda是的,但您只能将整个树作为子模块使用——我已经在答案中说明了这一点。OP想要指向一个文件,而您不能使用子模块。很抱歉,您的回答没有给对话添加任何内容。我的回答介绍了两种不同的解决方案,用于使用子模块指向文件。我对它进行了大量的编辑,我希望现在的意图更加清晰。这个答案是关于内部符号链接,而不是外部资源的符号链接,请看我的答案。真的吗?请再读一遍标题,然后读第一句话。我不明白你的意思。另外,如果你投了反对票,如果那是你的话,你也可以删除一条解释反对票的评论。否决权是一种QA工具,而不是复仇的工具。@PavelŠimerda当我在t