Upload a file to a Gist with bash

I usually paste error reports and logs on Gist at Github, to exchange programming relevant debug information. Gist doesn’t have a button to upload a file. So sometimes it is not so convenient to copy and paste your large errorreports into gists textarea for input.

Is there a way to upload a file from the commandline into a new Gist in your Gist account?

  • Checkout branch from Github without cloning whole repo, and without Git
  • How can you automatically remove trailing whitespace in vim
  • automated push to a github repo with travis
  • YAML parser from bash
  • How to set git hook over HTTP protocol to Automate development on a remote server
  • MongoDB backup as text rather than binary for source control
  • also creating a temporary git repository for the file to upload would help, I would automate this in a script then.

    In the end I would like to automate posting debug information of my programming project on github with one bash script

  • Git Renaming Issues
  • How can I get coloured patches with git interactive on bash?
  • Git: reset --hard now I'm stuck in the past
  • Can MKS Integrity integrate with other source control tools? (SVN, Git…)
  • Why is this a bad config line for my .gitconfig?
  • How to release with Git?
  • 3 Solutions collect form web for “Upload a file to a Gist with bash”

    Here is a solution that works for me on Bash/Dash to create anonymous gist (very probably not bullet-proof):

    # 0. Your file name
    FNAME=some.file
    
    # 1. Somehow sanitize the file content
    #    Remove \r (from Windows end-of-lines),
    #    Replace tabs by \t
    #    Replace " by \"
    #    Replace EOL by \n
    CONTENT=$(sed -e 's/\r//' -e's/\t/\\t/g' -e 's/"/\\"/g' "${FNAME}" | awk '{ printf($0 "\\n") }')
    
    # 2. Build the JSON request
    read -r -d '' DESC <<EOF
    {
      "description": "some description",
      "public": true,
      "files": {
        "${FNAME}": {
          "content": "${CONTENT}"
        }
      }
    }
    EOF
    
    # 3. Use curl to send a POST request
    curl -X POST -d "${DESC}" "https://api.github.com/gists"
    

    If you need to create a gist associated with your github account, (for basic authentication) replace the last line by:

    curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists"
    

    For more advanced authentification schemes, please see https://developer.github.com/v3/#authentication

    See https://github.com/defunkt/gist

    It’s a Ruby script that can be used from the command line.

    You should be able to create a new Gist, using the GitHub API for creating a Gist:

    POST /gists
    

    You will find various script using this API, like:

    • this gist.el emacs function.
    • this php script
    • this curl command

    Even the GitHub editor Atom.io has a gist-it feature.

    https://raw.githubusercontent.com/rpowelll/gist-it/master/media/screencast.gif

    Git Baby is a git and github fan, let's start git clone.