Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a single file from a remote git repository

Is there a way to programmatically download a single file from a remote git repository, in Java?

  1. I prefer a solution which uses as little bandwidth as possible, preferably only downloading that single file. I do not need to browse the repository, I already have the file's path.
  2. I prefer a solution which does not depend on other applications (e.g. an installation of another git client on the machine). A Java library which contains a git client implementation itself would be optimal.

I was able to do something similar with Subversion using SVNKit and I've seen there is a pure java implementation of git (eclipse's JGit) which might be able to do something similar, so I hope there is a positive answer; though from what I understand about how git works - allowing updates only from local repositories - this could prove to be problematic.

like image 269
Oak Avatar asked Sep 04 '10 11:09

Oak


People also ask

How do I pull just one file from a git repository?

Short Answergit checkout origin/master -- path/to/file // git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).

Can I clone a single file from git?

You can't clone a single file using git. Git is a distributed version control system, the Idea behind its clone functionality is to have a complete copy of project and all versions of files related to that project.

How do I pull a single branch from a remote?

You can fetch a specific branch from remote with git fetch <remote_name> <branch_name> only if the branch is already on the tracking branch list (you can check it with git branch -r ).


2 Answers

git isn't really designed for single file access from a remote repository but you can abuse git archive for this. The downside is that you have to download a "tree" rather than just the blob that you need.

E.g.

git archive --remote=url://to.git.repo branch path/to/dir | tar -x file

As an alternative, if you have gitweb set up on the remote repository you can use a simple curl or wget command to download any file in its "raw" format.

like image 126
CB Bailey Avatar answered Sep 19 '22 00:09

CB Bailey


What sort of access to do you have to the remote repository? Is it via SSH, can you call commands? If so, you can just invoke git show HEAD:$path_to_file.

If you cannot invoke commands on that machine, it is still entirely possible to do this, but you'll have to understand the Git repository format. (That’s much less scary than it sounds, since it is very simple by design. Unlike eg. Subversion, the repository format is not intended as a black box.)

like image 26
Aristotle Pagaltzis Avatar answered Sep 21 '22 00:09

Aristotle Pagaltzis