Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking a single file from another git repository

How do you link a single file from another git repository to your own repository? I don't want the full repository, just a single file. Using git submodule seems like the right route to go, but it wants to grab the whole thing.

like image 433
SineSwiper Avatar asked Sep 29 '11 13:09

SineSwiper


People also ask

Can I pull a single file from a git repository?

Conclusion. You can download an individual file from a GitHub repository from the web interface, by using a URL, or from the command line. You can only retrieve public files by URL or from the command line.

How do I link a git repo to another repo?

To do this, run the git subtree add command. This command will embed the master branch of Tim Pope's VIM-sensible in your dotfiles. Also note that you used the --squash flag to compress the history. You can run this command several times to embed multiple repositories.


1 Answers

Considering that the unit of work for git is a repository (or more precisely a repository content), I don't think you can easily integrate one file.

If you don't need its history, you could consider simply copy it in your repo.
But if you do need the history, then some git filter-branch (as in "git: How to split off library from project? filter-branch, subtree?") are in order. That seems a lot of effort for just one file though.

In theory, "git: symlink/reference to a file in an external repository" suggests a solution combining submodule and symlink.
(from Pavel Šimerda)

$ git submodule add /url/submodule/<reponame> $ ln -s <reponame>/path/to/<linked_file> $ git add .gitmodules <linked_file> $ git commit -m "add a symbolic link to <linked_file> with the respective submodule" 

Since 2011 (original answer above), Gavriel reports in the comments that:

  • When I checkout the main repo, the linked file is not a symlink, but instead the file content is "<reponame>/path/to/<linked_file>". In other words git commits the symlink's target as file content
  • But the basic idea works.
    I use "submodule/path/to/file" instead of "file" and it works
like image 191
VonC Avatar answered Sep 18 '22 08:09

VonC