Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created March 10, 2014 23:34
Show Gist options
  • Save madrobby/9476733 to your computer and use it in GitHub Desktop.
Save madrobby/9476733 to your computer and use it in GitHub Desktop.
Download a single file from a private GitHub repo. You'll need an access token as described in this GitHub Help article: https://help.github.com/articles/creating-an-access-token-for-command-line-use
curl -H 'Authorization: token INSERTACCESSTOKENHERE' -H 'Accept: application/vnd.github.v3.raw' -O -L https://api.github.com/repos/owner/repo/contents/path
@peteratticusberg
Copy link

I found this last part a tad confusing:
-L https://api.github.com/repos/owner/repo/contents/path

it should be used as follows:

-L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE
e.g.
-L https://api.github.com/repos/acme_corp/flagship_project/contents/app/views/users/show.html.erb

@13rac1
Copy link

13rac1 commented Sep 23, 2017

v4 is available, just change to Accept: application/vnd.github.v4.raw

@mehta-m
Copy link

mehta-m commented Apr 13, 2018

When I run this in powershell - i get the following error :
Invoke-WebRequest : Cannot bind parameter 'Headers'. Cannot convert the "Authorization: token
MyToken" value of type "System.String" to type "System.Collections.IDictionary".
At line:1 char:9
curl -H 'Authorization: token MyToken ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException
FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

@itscaro
Copy link

itscaro commented Apr 14, 2018

@mehta-m curl in powershell is an alias to Invoke-WebRequest, it's not the curl command as in linux. You can install curl for windows to use curl in powershell

@vipulkumarsviit
Copy link

Is there any way to download a folder in the same way

@ksridhar
Copy link

ksridhar commented Jan 23, 2019

thanks to
@peteratticusberg commented on Aug 9, 2015
would like to add that if you want to retrieve a file stored in a specific tag (say: my_unit_test) you would specify the curl command as follows (as in a bash script):

USER="me"
PASSWD="mypasswd"
OUTPUT_FILEPATH="./foo"
OWNER="mycompany"
REPOSITORY="boo"
RESOURCE_PATH="project-x/a/b/c.py"
TAG="my_unit_test"
curl \
    -u "$USER:$PASSWD" \
    -H 'Accept: application/vnd.github.v4.raw' \
    -o "$OUTPUT_FILEPATH" \
    -L "https://api.github.com/repos/$OWNER/$REPOSITORY/contents/$RESOURCE_PATH?ref=$TAG"

@emmahsax
Copy link

emmahsax commented Nov 22, 2019

Does anyone else run into the issue where when they get the zipped or tarred file, they cannot unzip it (or untar it)?

$ curl -H 'Authorization: token my-oauth-token-here' -H 'Accept: application/vnd.github.v3.raw' -O -L https://api.github.com/repos/owner-name/repo-name/releases/assets/v1.1.0.tar.gz
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   130  100   130    0     0    473      0 --:--:-- --:--:-- --:--:--   474
$ ls
v1.1.0.tar.gz
$ tar xvzf v1.1.0.tar.gz 
tar: Unrecognized archive format
tar: Error exit delayed from previous errors.

Somehow, using the command line doesn't get the full zipped/tarred file. when I click the link in the UI, I can successful unzip/untar the file via the command line after it downloads to my machine.

@emmahsax
Copy link

emmahsax commented Nov 22, 2019

Ah I figured it out. This works!

ACCESS_TOKEN='1234567890abcdefghijk'
REPO_DOWNLOAD_URL=$(curl -u "${ACCESS_TOKEN}:" -s https://api.github.com/repos/owner-name/repo-name/releases/latest | \
  awk '/tag_name/ {print "https://api.github.com/repos/owner-name/repo-name/tarball/" substr($2, 2, length($2)-3) ""}'
)

curl -u "${ACCESS_TOKEN}:" -LkSs "{$REPO_DOWNLOAD_URL}" -o - | tar xzf -

This finds the latest release for repository owner-name/repo-name, and then grabs the tar of it and opens it up in the current directory. A user just needs an Oauth access token!

@jakeonfire
Copy link

jakeonfire commented Dec 24, 2020

when using -O to save a file with the same name, it includes request params in the file name. so i prefer setting ref via -d like this:

curl -GLOf -H "Authorization: token ${GITHUB_TOKEN?not set}" -H "Accept: application/vnd.github.v4.raw" \
  "https://api.github.com/repos/$ORG/$REPO/contents/$FILEPATH" -d ref="$REVISION"

-G causes data (specified by -d) to be added as URL request params, but not the saved filename. -f ensures if there is an issue, like 401 bad auth, the command returns an error instead of just saving the file with the error response.

@porthunt
Copy link

porthunt commented Aug 6, 2021

I've created this simple gist in case someone needs to download a json file from a private repository and use it as a dictionary: https://gist.github.com/porthunt/b994154c054deeab7ab4073273aa75bc

@W1BTR
Copy link

W1BTR commented Jun 13, 2024

I get:

curl: (6) Could not resolve host: application
{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/repos/contents#get-repository-content",
  "status": "404"
}

I'm on windows, if that makes a difference.

My command is structured as follow:
curl -H 'Fine_Grain_Auth_Token_With_Readonly_permissions' -H 'Accept: application/vnd.github.v4.raw' -L https://api.github.com/repos/MYACCOUNT/MYREPO/contents/Bin/file.txt

I am able to access the file with a url like this (when signed in)
https://raw.githubusercontent.com/MYACCOUNT/MYREPO/main/Bin/file.txt
I have also tried having the /main in the url in the command with no change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment