Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@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

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