git:symlink /对外部存储库中文件的引用

Hil*_*kus 29 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/logback.xml)
Run Code Online (Sandbox Code Playgroud)

可以看出,在这种情况下,拥有一个完整的子模块文件夹是没有意义的,它只是一个文件.
我很好,链接被锁定到一个特定的提交,但如果它在它自己的项目的生命周期中发生变化,它会更好

需要注意的是,这与文件系统的符号链接无关; 我在谈论对另一个项目,repo,branch或任何东西中的文件的引用.如果文件的内容是重复的而不是文件系统符号链接,那就没问题

Pav*_*rda 40

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

每个外部git树应该有一个子模块,您应该小心处理子模块,因为它们不仅是指向外部存储库的链接,还指向它们的特定提交.以下解决方案将向您展示如何使用外部存储库中的单个文件,但仍保留子模块的所有优点.

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

使用子模块和符号链接

准备好子模块后,只需添加指向子模块目录结构的文件系统符号链接即可.

在项目目录的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"
Run Code Online (Sandbox Code Playgroud)

现在你有了一个符号链接:

logback.xml -> JRoboCom/jrobocom-core/src/main/resources/logback.xml
Run Code Online (Sandbox Code Playgroud)

使用子模块和脚本

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

创建一个bootstrap.sh包含以下内容

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

cp JRoboCom/jrobocom-core/src/main/resources/logback.xml .
Run Code Online (Sandbox Code Playgroud)

在项目目录的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"
Run Code Online (Sandbox Code Playgroud)

请注意,我们没有将logback.xml文件添加到Git,因为它将从子模块中获取.

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

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

找到了Git> = 1.7.9.5的另一个解决方案git archive.

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

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

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

$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from the remote project"
Run Code Online (Sandbox Code Playgroud)

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

如果存储库托管服务也通过HTTP提供单个文件,您只需使用curlwget下载它们即可.

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

#!/bin/sh
curl -O https://raw.githubusercontent.com/theHilikus/JRoboCom/master/jrobocom-core/src/main/resources/logback.xml
Run Code Online (Sandbox Code Playgroud)

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

$ git add bootstrap.sh
$ git commit -m "add a script to fetch logback.xml directly from github"
Run Code Online (Sandbox Code Playgroud)

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.

  • 由于使用符号链接,我猜这在 Windows 中不起作用?或者是否有 Windows 解决方法? (2认同)