Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I download a single raw file from a private github repo using the command line?

Tags:

github

curl

oauth

On the CI server, I want to fetch a config file that we maintain on Github so it can be shared between many jobs. I'm trying to get this file via curl, but these approaches both fail (I get a 404):

# As advised by the oAuth docs curl -H 'Authorization: token the_token' -L -o setup.sh https://raw.github.com/org/repo/file  # The url of the raw file after clicking to view it curl -L https://raw.github.com/org/repo/file?login=username&token=the_token  
like image 927
Matt Gibson Avatar asked Aug 08 '13 12:08

Matt Gibson


People also ask

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. Now, type in “git clone https://github.com/bdward16/tip-calculator.git“and hit Enter.

How do I download individual files from repository?

Downloading From The Command Line You will need to create a personal access token to use the API, with which you can replace “ACCESS_TOKEN” in this script. You can then use a JSON parser like jq to pull out the URL and download it. To get the repository file list recursively, you will need to fetch the root tree.

How do I download a single file from GitHub Linux?

If it's just a single file, you can go to your GitHub repo, find the file in question, click on it, and then click “View Raw”, “Download” or similar to obtain a raw/downloaded copy of the file and then manually transfer it to your target server.


1 Answers

The previous answers don't work (or don't work anymore).

You can use the V3 API to get a raw file like this (you'll need an OAuth token):

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

All of this has to go on one line. The -O option saves the file in the current directory. You can use -o filename to specify a different filename.

To get the OAuth token follow the instructions here:

  • https://help.github.com/articles/creating-an-access-token-for-command-line-use

I've written this up as a gist as well:

  • https://gist.github.com/madrobby/9476733

EDIT: API references for the solution are as follows:

  • https://developer.github.com/v3/#authentication
  • https://developer.github.com/v3/media/#request-specific-version
  • https://developer.github.com/v3/repos/contents/#get-contents
like image 144
thomasfuchs Avatar answered Sep 21 '22 15:09

thomasfuchs