How can I download a single raw file from a private github repo using the command line?
CurlGithubOauthCurl 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
.
- First you must be logged in :
gh auth login
For me, this command is not required since I'm already logged in.
- 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
iscli
- Repository name
repo
iscli
too - Path to file (
path/file.ext
) isdocs/install_linux.md
Solution 7 - Curl
- in browser open your github repo: click on file
- open Developer Tools in browser: select Network tab
- in browser github: click on Download button
- close pop-up
- in browser dev tools: right click on list that has
file_name?token=ABAHQCAT6KG...
- select copy -> copy link address
url is in format:
https://raw.githubusercontent.com/<USERNAME>/<PATH>/<FILENAME>?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I
-
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.
- Create personal token, followed these steps:
https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
-
Make sure you have minimum following permissions for the token:
-
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
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.
- Go to source code page what you want to download via curl or wget, etc
- Find 'raw' button and click it.
- New page opened and just copy the url. This url look like below:
(https://raw.githubusercontent.com/USERNAME/REPONAME/BRANCHNAME/FILENAME?token=TOKENSTRING). - 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.
- 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": []
}]
}
-
open notepad/text editor on os(eg. windows) copy all the text to a new notepad file .
-
save notepad as name.ipynb instead of name.txt and make save as file type All Files(.) instead of Text Documents (*.txt)
-
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