Skip to content

Instantly share code, notes, and snippets.

@MichaelPaulukonis
Last active March 14, 2025 15:23
Show Gist options
  • Save MichaelPaulukonis/2fa0093fe39acc9189c305a21b0f50cb to your computer and use it in GitHub Desktop.
Save MichaelPaulukonis/2fa0093fe39acc9189c305a21b0f50cb to your computer and use it in GitHub Desktop.
How to make a gist from the command-line

create a GitHub gist from the shell

See docs @ https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28#create-a-gist

Create the JSON payload with escaped content

content=$(awk '{gsub(/\\/, "\\\\"); gsub(/"/, "\\\""); printf "%s\\n", $0}'  /path/to/your/file | sed ':a;N;$!ba;s/\n/\\n/g')

cat <<EOF > payload.json
{
  "description": "<description>",
  "public": true,
  "files": {
    "filename.ext": {
      "content": "$content"
    }
  }
}
EOF

Use curl to create the Gist

NOTE: This uses a fine-grained access token with gist permissions

curl -L \
  -X POST \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <your_token>" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  https://api.github.com/gists \
  -d @payload.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment