Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download specific files from github in command line, not clone the entire repo

Tags:

How do I download just 2 files from github using command line ?
Something in the lines of :

git fetch git://github.com/username/Project.git/file1 git fetch git://github.com/username/Project.git/file2 
like image 457
anjanesh Avatar asked Feb 06 '12 12:02

anjanesh


People also ask

How do I download only part of a GitHub repository?

GitHub User InterfaceThere's a download button on the repository's homepage. Of course, this downloads the entire repo, after which you would need to unzip the download and then manually drag out the specific folder you need.

How do I download a git repository without cloning?

If you DO NOT see those options then you must click the directory link above the viewing area (i.e. some name > some folder > some other folder) to essentially back out of the directory until you see the option to "Download" and that is the entire compressed file to be downloaded.

How do I download individual files from repository?

Downloading a Single File From The Github Website You can copy/paste from here, but in most browsers, you should be able to right click and select “Save As” to download the file directly. For code files, it may try to save as . txt , which you will need to fix manually before or after downloading.

How do I download from GitHub command line?

Open up Git Bash, type in “cd Downloads” and hit Enter. This will take you to the Downloads folder in the command window, you can also type whatever file location you want to save the file in.


1 Answers

If you go to the page and view the links provided by "raw" (in the top left corner, when viewing the file). You will see, that you can access it by:

https://github.com/username/repository/raw/$changeset_hash/path/to/file 

Instead of $changeset_hash you can also provide a branch (e.g. master) or tag.

You can retrieve the raw file using something like wget.

Accessing a single file directly from a .git-repository is not possible (as far as I know), because of how the data is stored.

edit: When you want to access a file from a private repo, you first have to create an access token with the appropriate permissions in your account settings. Instead of calling the url above you can then use github's API to access the content of a file. Be sure to use the Accept-header for custom media types to get the raw data. This might look something like this:

curl \   -H 'Authorization: token $YOUR_TOKEN' \   -H 'Accept: application/vnd.github.v3.raw' \   -O \   -L 'https://api.github.com/repos/:owner/:repo/contents/:path' 

The -O will save the contents in a local file with the same name as the remote file name. For easier use you can wrap it in a script. @Chris_Withers suggested an edit with a nice python snippet that unfortunately got rejected as to big of a change to the answer.

like image 185
dbrumann Avatar answered Oct 27 '22 17:10

dbrumann