git:外部存储库中文件的符号链接/引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15844542/
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
git: symlink/reference to a file in an external repository
提问by Hilikus
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:
在 git 中是否有可能在 git repo 中拥有到特定文件的“链接”?就像 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)
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
请注意,这与文件系统的符号链接无关;我说的是对另一个项目、存储库、分支或任何东西中的文件的引用。如果文件的内容是重复的而不是文件系统符号链接,那没关系
回答by Pavel ?imerda
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.
Git 具有可用于实现所需功能的功能。它支持文件系统符号链接并支持子模块。子模块已经是处理对其他存储库的引用的标准方法。您可以将它们与在本地引用文件的方法结合使用。这可以使用相对符号链接直接处理,也可以使用脚本间接处理,该脚本将文件从子模块复制到您需要的位置。
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.
每个外部 git 树都应该有一个子模块,并且应该小心对待这些子模块,因为它们不仅是指向外部存储库的链接,而且是指向它们特定提交的链接。以下解决方案将向您展示如何使用来自外部存储库的单个文件,但仍保持子模块的所有优点。
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 avoiddownloading 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:
在项目目录中的 shell 中运行它:
$ 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:
创建一个bootstrap.sh
包含以下内容的文件:
#!/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:
在项目目录中的 shell 中运行它:
$ 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.
请注意,我们不会将logback.xml
文件添加到 Git,因为它将从子模块中获取。
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
使用脚本通过 git 协议获取单个文件
Found another solution for Git >= 1.7.9.5 using git archive
.
为 Git >= 1.7.9.5 找到了另一个使用git archive
.
Create a file bootstrap.sh containing:
创建一个包含以下内容的文件 bootstrap.sh:
#!/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:
在项目目录中的 shell 中运行它:
$ 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
使用脚本通过 HTTP 获取单个文件
If the repository hosting service also serves the individual files via HTTP, you can simply use curl
or wget
to download them.
如果存储库托管服务也通过 HTTP 提供单个文件,您可以简单地使用curl
或wget
下载它们。
Create a file bootstrap.sh containing:
创建一个包含以下内容的文件 bootstrap.sh:
#!/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:
在项目目录中的 shell 中运行它:
$ 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.
您还可以将引用存储在具有特定扩展名(如*.url
)的文件中,或将引用列表保存在一个文件中(如.references
在您的项目目录中),并构建一个更全面的脚本来遍历所有引用并下载相应的文件。
回答by Jeach
The accepted answer seems misleading since Git can handle symlinks just fineas long as the operating system used by all developers supports them.
接受的答案似乎具有误导性,因为只要所有开发人员使用的操作系统都支持它们,Git 就可以很好地处理符号链接。
Git by default attempts to store symlinks instead of following them (for compactness and its generally what people want).
默认情况下,Git 尝试存储符号链接而不是跟随它们(为了紧凑性和人们通常想要的)。
When you checkout a tree containing the link, it restores the object as a symlink regardless of whether the target file system object exists or not.
当您检出包含链接的树时,无论目标文件系统对象是否存在,它都会将对象恢复为符号链接。
If you are on filesystem like FAT that does not support symbolic links, and your repository uses them, you can set core.symlinks configuration variable to false, and symlinks would be checked out as small plain text files that contain the link text.
如果您在像 FAT 这样不支持符号链接的文件系统上,并且您的存储库使用它们,您可以将 core.symlinks 配置变量设置为 false,并且符号链接将作为包含链接文本的小型纯文本文件被检出。
SO References:
参考资料:
How does git handle symbolic links?
回答by John Szakmeister
Unfortunately, no. The only type of external reference you can have in git is through submodules, which puts the entire branch in a directory. You'll need to create a script or something to help fetch the file from the desired location and put it in your working tree.
抱歉不行。您可以在 git 中拥有的唯一外部引用类型是通过子模块,它将整个分支放在一个目录中。您需要创建一个脚本或其他东西来帮助从所需位置获取文件并将其放入您的工作树中。