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 d
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 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.
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
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.
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"
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"
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.
The accepted answer seems misleading since Git can handle symlinks just fine as long as the operating system used by all developers supports them.
Git by default attempts to store symlinks instead of following them (for compactness and its generally what people want).
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.
SO References:
How does git handle symbolic links?
Git - how to handle symlinks
How can I get git to follow symlinks?