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

CurlGithubOauth

Curl Problem Overview


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 

Curl Solutions


Solution 1 - Curl

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:

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

EDIT: API references for the solution are as follows:

Solution 2 - Curl

Alternatively, you can use a github "personal access token" (https://github.com/settings/tokens):

TOKEN=...
curl -s https://[email protected]/<user or organization>/<repo name>/<branch>/<path to file>/<file_name>

Example:

$ curl -s https://[email protected]/concourse/concourse/master/README.md
....

Solution 3 - Curl

I know this is an old question, but none of the solutions proposed above worked for me. Perhaps the API has changed since then.

This worked:

curl -H 'Authorization: token [insert your token here]' -o output.txt https://raw.githubusercontent.com/[organization]/[repo]/[branch]/[path to file]

Solution 4 - Curl

Or, if you don't have a token:

curl --user [your_user] 'https://raw.github.com/path/to/file.config' > file.config

Solution 5 - Curl

I was struggling with this for a few minutes until I realized all that is needed is to wrap the url in quotes to escape the ampersand.

curl "https://raw.github.com/org/repo/file?login=username&token=the_token"

That worked for me in my private repo.

Solution 6 - Curl

A bit simpler solution IMHO is to use Official GitHub CLI gh.

  1. First you must be logged in :
gh auth login

For me, this command is not required since I'm already logged in.

  1. Then we need API URL targeting the file to download. And call gh to convert it to authentified download URL:
API_URL=https://api.github.com/repos/owner/repo/contents/path/file.ext
curl $(gh api $API_URL --jq .download_url) -o file.ext

An real example is maybe better. Here it is to download install_linux.md from gh cli:

API_URL=https://api.github.com/repos/cli/cli/contents/docs/install_linux.md
curl $(gh api $API_URL --jq .download_url) -o install_linux.md

In API_URL:

  • User owner is cli
  • Repository name repo is cli too
  • Path to file (path/file.ext) is docs/install_linux.md

Solution 7 - Curl

  1. in browser open your github repo: click on file
  2. open Developer Tools in browser: select Network tab
  3. in browser github: click on Download button
  4. close pop-up
  5. in browser dev tools: right click on list that has file_name?token=ABAHQCAT6KG...
  6. select copy -> copy link address

url is in format:

https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I

  1. in terminal:

    wget -O myFilename https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I

Link is valid only for limited time or you can create your token: GitHub article

Solution 8 - Curl

I ran into an authentication error when the url was redirected to Amazon S3:

> Only one auth mechanism allowed; only the X-Amz-Algorithm query parameter...

Changing from the Authorization: token X header to the ?access_token=<token> query param worked for me.

Solution 9 - Curl

I was able to get it to work for github enterprise, thanks for the suggestions above. Had to take all your suggestions and try and finally i was able to make it work. These are the steps i followed to get it to work.

  1. Create personal token, followed these steps:

https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token

  1. Make sure you have minimum following permissions for the token:

    • repo (Select all under repo)
    • admin:org -> read:org (select "read:org" under "admin:org") enter image description here
  2. Use the following curl command to get the content:

curl -H "Authorization: token [yourPersonalToken]" -H "Accept: application/vnd.github.v3.raw" -o [filePath]-content.json -L https://github.[company].com/api/v3/repos/[ORG]/[REPO_NAME]/contents/[PATH_TO_FILE]/content.json?ref=[BRANCH_NAME]

Where->

 [yourPersonalToken] is the token you created.
 [filePath] is a path where you want to save the downloaded copy.
 [company] is the name of company which hosted the github enterprise.
 [ORG] is the github organization is which repo is created.
 [REPO_NAME] is the name of the repository.
 [PATH_TO_FILE] is the path where file is located.
 [BRANCH_NAME] is the name of the branch you want to use, e.g. master, develop etc.

Example:

curl -H "Authorization: token 5a86ecda9ff927baaa66fad2af5bee8" -H "Accept: application/vnd.github.v3.raw" -o C:\Downloads\manifest.json -L https://github.example.com/api/v3/repos/cms/cms_one/contents/app/data/manifest.json?ref=master

Solution 10 - Curl

We had to download files from private GitHub repos fairly often and hacky shell scripts weren't quite cutting it, so we created fetch, which is an open source, cross-platform tool that makes it easy to download source files and release assets from a git tag, commit, or branch of public and private GitHub repos.

For example, to download the file baz from version 0.1.3 of a private GitHub repo to /tmp, you would do the following:

GITHUB_OAUTH_TOKEN="your token"
fetch --repo="https://github.com/foo/bar" --tag="0.1.3" --source-path="/baz" /tmp

Solution 11 - Curl

Just an addition to the accepted answer, If you are using Github Enterprise url is slightly different:

curl -H 'Authorization: token [your token]' \
-H 'Accept: application/vnd.github.v3.raw' \
-L https://[your domain]/api/v3/repos/[owner]/[repo-name]/contents/[path of file]

Solution 12 - Curl

Surprisingly none of the answers didn't worked for me until I found a workaround.

You can use personal access token https://github.com/settings/tokens as answered by @thomasfuchs

Note : while creating a token, you must check the admin permissions. See the related issue

https://github.com/octokit/octokit.net/issues/1812

Solution 13 - Curl

I think it is a little bit dangerous and not good way to issue a personal access token which can access all repositories even just for download a single file from my private repository.

How to -

I would love to recommend using url with token for single file. Don't worry. The token string will generated by github automatically. You can get this url on your source code page.

  1. Go to source code page what you want to download via curl or wget, etc
  2. Find 'raw' button and click it. enter image description here
  3. New page opened and just copy the url. This url look like below:
    (https://raw.githubusercontent.com/USERNAME/REPONAME/BRANCHNAME/FILENAME?token=TOKENSTRING).
  4. You can download a file using this url

Solution 14 - Curl

curl -H 'Authorization: token YOUR_TOKEN' \
  -H 'Accept: application/vnd.github.v4.raw' \
  -O \
  -L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE

So if the url of the raw file (when logged in) is

https://raw.githubusercontent.com/mr_coder/my_repo_name/master/my_script


Then 
  -L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE
becomes
  -L https://api.github.com/repos/mr_coder/my_repo_name/contents/my_script

Note: We have API v4

Solution 15 - Curl

For GitHub Enterprise and API v3, my bash solution looked like this (includes TOKEN cleanup / privacy):

TOKEN=yourTokenHere; history -d $((HISTCMD-1)) > /dev/null

curl -H "Authorization: token $TOKEN" \
  -H 'Accept: application/vnd.github.v3.raw' \
  -o file.ext \
  -L http://github.company.com/api/v3/repos/[org]/[repo]/contents/path/file.ext?ref=[branch]

unset TOKEN

Solution 16 - Curl

I tried a simple trick to open a GitHub private .iypnb file in Pycharm as well as Colab and it worked well for me.

  1. get raw text for your .ipynb file by pressing Raw button this will open some text like this.
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": []
}]
}
  1. open notepad/text editor on os(eg. windows) copy all the text to a new notepad file .

  2. save notepad as name.ipynb instead of name.txt and make save as file type All Files(.) instead of Text Documents (*.txt)

  3. finally open file in your IDE or colab.

Solution 17 - Curl

Below should work fine. A "raw" before your branch name (master in this case).

curl -L -O https://github.com/your/repo/raw/master/fetch_file.sh

Solution 18 - Curl

You can do this with a raw link.

curl -O https://raw.githubusercontent.com/owner/repo/branchname/path/to/file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt GibsonView Question on Stackoverflow
Solution 1 - CurlthomasfuchsView Answer on Stackoverflow
Solution 2 - CurltheartofrainView Answer on Stackoverflow
Solution 3 - Curlmark amosView Answer on Stackoverflow
Solution 4 - CurlPeqNPView Answer on Stackoverflow
Solution 5 - CurlPhilip ForgetView Answer on Stackoverflow
Solution 6 - CurlJean-Pierre MatsumotoView Answer on Stackoverflow
Solution 7 - CurlMilan RakosView Answer on Stackoverflow
Solution 8 - CurlchrismoView Answer on Stackoverflow
Solution 9 - CurlAbhinav MishraView Answer on Stackoverflow
Solution 10 - CurlYevgeniy BrikmanView Answer on Stackoverflow
Solution 11 - Curllonewarrior556View Answer on Stackoverflow
Solution 12 - CurlatulView Answer on Stackoverflow
Solution 13 - CurlTonyView Answer on Stackoverflow
Solution 14 - CurlRubView Answer on Stackoverflow
Solution 15 - CurlGeoffrey HudikView Answer on Stackoverflow
Solution 16 - CurlPruthviraj JadhavView Answer on Stackoverflow
Solution 17 - CurlSubhakar K SView Answer on Stackoverflow
Solution 18 - CurlSahak KhotsView Answer on Stackoverflow