30

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?

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

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
rubo77
  • 19,527
  • 31
  • 134
  • 226
  • 1
    Not a programming question, there's also a button that says "add file" on the page you link to. – Dr.Avalanche Oct 21 '14 at 10:22
  • 4
    This is a programming question, because gist is the biggest platform for Programmers. besides: there is an "Add File" button but that doesn't upload a file, it opens another textarea to **Paste** another file – rubo77 Oct 21 '14 at 10:23
  • I clarified the purpose of my question, please reconsider your votes – rubo77 Oct 21 '14 at 10:31
  • related: [How do you upload images to a gist?](http://stackoverflow.com/questions/16425770/how-do-you-upload-images-to-a-gist) – rubo77 Oct 21 '14 at 11:19
  • can you not use pastebinit to upload to gist? https://bugs.launchpad.net/pastebinit/+bug/268287 – Thufir Oct 23 '17 at 11:46

7 Answers7

15

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

rubo77
  • 19,527
  • 31
  • 134
  • 226
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
  • @Elric [Done !](https://gist.github.com/s-leroux/7cb7424d33ba3753e907cc2553bcd1ba) – Sylvain Leroux Jun 24 '16 at 09:46
  • I enhanced it a bit: https://gist.github.com/rubo77/3b8b15f4df5b5287a24f87523c41e4ce – rubo77 Nov 19 '16 at 08:01
  • There is one problem: if the file you are uploading contains a `%` character, it seems to break at that point: I tried to upload a file containing this: `UNIX_TIMESTAMP=$(date +%s%3N --date='TZ="UTC+2" '"$TIME") ` – rubo77 Nov 19 '16 at 08:02
  • @rubo77 Interesting. Right now, I can't see why `%` could have a special meaning. I don't know if I will have time to investigate that. Keep me informed if you have more ideas ! – Sylvain Leroux Nov 19 '16 at 15:21
  • 2
    Why do you use gist for that? Lets move on with a repo that we can track its issues: https://github.com/ceremcem/create-gist – ceremcem Sep 12 '17 at 19:28
  • **sanitize** needs to be improved, code that contains literal `\n` becomes a newline in the uploaded gist, similar issues for literals `\t` and `\"`. Also commands that are spread over multiple lines with a trailing backslash `\ ` are not handled well. And scripts that also use the same `EOF` heredoc terminator are only partly uploaded. – Pro Backup Sep 04 '18 at 22:42
  • To save people some time, go to directly to https://stackoverflow.com/a/68461779/610569 that provided an official github CLI usage. – alvas Jul 28 '22 at 04:28
11

A command line tool implemented in Python:

pip install python-gist
gist create "example description" foo.txt bar.txt

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

If you prefer a Ruby gem, see https://github.com/defunkt/gist

Andreas
  • 550
  • 3
  • 12
  • 1
    on Ubuntu: `apt install gist` gives the gem. I'm sure you can install with ruby if you prefer. works like a gem :) – Thufir Oct 23 '17 at 13:03
7

Using the GitHub CLI:

gh gist create yourfile

Or, for a public gist,

gh gist create --public yourfile

To add a description, use the --desc/-d flag, and to open directly in a web browser, use --web/-w.

See the reference manual for gh gist create.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
5

Building on the answer of Sylvain Leroux, we can replace the sanitization and json building steps by making use of the jq command line tool:

$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -X POST -d @- "https://api.github.com/gists"

Or, with authentication:

$ jq -Rs '{"description": "some description", "public": true, "files": {"'$FNAME'": {"content": .}}}' $FNAME | curl -u "${GITHUB_USERNAME}" -X POST -d @- "https://api.github.com/gists"
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Tim Rakowski
  • 97
  • 2
  • 7
3

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:

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

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

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

Here is a Python script to do the same. It is actively developed by me. The README is pretty straightforward in its usage details. Some examples-

Get a list of gists

gifc get 5

Create a gist

  • Create interactively from an editor like nano, vim or gedit
    • gifc create create.md -d "How to create a gist from cli" -i nano
  • Directly enter contents from cli
    • gifc create create.md -d "How to create a gist from cli" -m '''If you want to create a gist from an existing file then you do the following- `gifc create create.md -d "How to create a gist from cli" -f file.md`'''
  • Take the contents from a file
    • gifc create create.md -d "How to create a gist from cli" -f file.md

Update a gist

  • Edit all (or some) files iteratively

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
      You can get the gist id from the get method from earlier
  • Change description

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
      You can get the gist id from the get method from earlier
  • Edit contents of a file interactively in an editor like nano, vim or gedit

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
  • Do both
    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"

Delete file(s) from a gist

gifc remove ffd2f4a482684f56bf33c8726cc6ae63 -r file1.md script.py readme.txt
You can get the gist id from the get method from earlier

Delete the whole gist

gifc delete ffd2f4a482684f56bf33c8726cc6ae63
You can get the gist id from the get method from earlier

jar
  • 2,646
  • 1
  • 22
  • 47
0

For routers with limited busybox ash shells I created this shell script post and patch gister. Usage: $ pgist my_file_to_post_or_patch_to_gist.extension

No need to remember long gist ID's. This gister is coded to automagically lookup the corresponding gist ID. Limitations are:

  1. up to 30 (maybe 100) gists in your account
  2. up to 300 files per gist
  3. unique filenames for all your 30×300 gist files
  4. it's not able to post/patch itself due to "Problems parsing JSON"

Installation example

curl -O https://gist.githubusercontent.com/ProBackup-nl/3971a45b21749cfff6c0069d3dad1dde/raw/pgist.sh && chmod 755 pgist.sh && mv pgist.sh /opt/usr/sbin/pgist

Dependencies

  • github oauth token
  • jq
  • sed
  • awk
  • curl and ca-certificates to create a valid certificate chain instead of

(60) SSL certificate problem: unable to get local issuer certificate

Community
  • 1
  • 1
Pro Backup
  • 729
  • 14
  • 34