20

I want to upload a file on my desktop called 'hello.txt' to my git repository which has a release. How do I do this? I read the git documentation but it says something like :

POST https://<upload_url>/repos/:owner/:repo/releases/:id/assets?name. How to do this in CURL. I did not understand this.

How to post this file as a release asset to my github release? Thanks

4
  • superuser.com/q/149329/219423 is this what you are looking for?
    – 1615903
    Commented Jun 13, 2016 at 9:51
  • Not sure if it accepts that. Was looking for the complete git command so I could modify it according to my needs and understand it too
    – tsaebeht
    Commented Jun 13, 2016 at 10:03
  • In the question, you specifically said that you want to do this using cURL. Is that not the case?
    – 1615903
    Commented Jun 13, 2016 at 10:05
  • Yeah but what is this <upload_url>? How to upload a file on my desktop ? If you are aware of how to create a request I want, could you write an answer?
    – tsaebeht
    Commented Jun 13, 2016 at 10:06

2 Answers 2

26
curl \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "Content-Type: $(file -b --mime-type $FILE)" \
    --data-binary @$FILE \
    "https://uploads.github.com/repos/hubot/singularity/releases/123/assets?name=$(basename $FILE)"
1
  • 12
    If someone reaches here after trying this for enterprise version of github, you have a different upload url instead of 'uploads.github.com'. The upload url for your particular enterprise version would be returned as a parameter (upload_url) when creating a new release
    – akskap
    Commented May 5, 2017 at 8:54
8

Extending upon @galeksandrp's answer, here are some issues I encountered

Note that the --data-binary option copies the file contents first to RAM, so if you have large files say close to 2048MB i.e. the absolute limit for github releases and if the RAM isn't enough, it fails with curl: option -d: out of memory.

The fix for that is to use -T file path (without the @).

And also on a side note, if you want to see the upload progress, you need to pipe the output to cat such as curl <...the whole command> | cat

So the complete command would look like this

curl -X POST \
    -H "Content-Length: <file size in bytes>" \
    -H "Content-Type: $(file -b --mime-type $FILE)" \ #from @galeksandrp's answer
    -T "path/to/large/file.ext" \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "Accept: application/vnd.github.v3+json" \ 
    https://uploads.github.com/repos/<username>/<repo>/releases/<id>/assets?name=<name> | cat

Note: An alternate way is to use gh release upload <tag> <files>... [flags] instead of curl. (docs)

gh is GitHub's official command line tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.