downloading a single file from github with gh

May 23, 2023

gh api -H "Accept: application/vnd.github.raw+text" repos/<organization>/<repo>/contents/<file path and name>

for example, to download limbo's readme:

gh api -H "Accept: application/vnd.github.raw+text" repos/llimllib/limbo/contents/

If you don't include the Accept header, you'll get metadata about the file instead:

$ gh api repos/llimllib/limbo/contents/
  "name": "",
  "path": "",
  "sha": "f19cac71cafae51ae702da5f02c9be80a40cc2df",
  "size": 5374,
  "url": "",
  "html_url": "",
  "git_url": "",
  "download_url": "",
  "type": "file",
  "content": "IyBMaW1ibwoKIyMjIEEgW1NsYWNrXShodHRwczovL3NsYWNrLmNvbS8pIGNo\nYXRib3QKCiMjIFN0YXR1cwoKQXQgdGhlIG1vbWVudCwgSSBjb25zaWRlciBs\naW1ibyB0byBiZSBmZWF0dXJlIGNvbXBsZXRlLCBhbmQgdGhlIHByb2plY3Qg\naXMgaW4gbWFpbnRlbmFuY2UgbW9kZS4gRXZlcnkgb25jZSBpbiBhIHdoaWxl\nIEkgY29tZSBpbiBhbmQgdXBkYXRlIHRoZSBkZXBlbmRlbmNpZXMuCgpDb250\ncmlid>
  "encoding": "base64",
  "_links": {
    "self": "",
    "git": "",
    "html": ""

the URLs of privately available files will include token= parameters allowing you to download them with, for example wget $(gh api repos/organization/a_private_repo/contents/.secrets | jq -r .download_url)

↑ up