Upload a file to a Gist with bash

embed gist
gist documentation
gist python
gist pastebin
gist online
gist cli
gist binary file
anonymous gist

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

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

upload file to gist bash · GitHub, upload file to gist bash. upload_to_gist.sh. # 0. Your file name. FNAME=worker.​log. GITHUB_USERNAME=kigawas. # 1. Somehow sanitize the file content. $ gist.sh 1234 or to write the gist to a file $ gist.sh -f file 1234 or to write the gist to the clipboard (xclip must be available) $ gist.sh -c 1234 Cloning a gist from GitHub: $ gist.sh -l 1234 or to clone using the private clone URL to support local update and push (this requires the gist to have been created with authentication): $ gist.sh -p -l 1234

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

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

gist(1) - upload code to https://gist.github.com, Command. To upload the contents of a.rb just: gist a.rb. Upload multiple files: gist a b c gist *.rb. By default it reads from STDIN, and you can set a filename with -f  Shell script to upload a file to AWS S3 using Bash. - s3_upload.sh

gist - upload code to https://gist.github.com, rb · Upload multiple files: gist-paste a b c gist *.rb · By default it reads from STDIN, and you can set a filename with -f. gist-paste -f test.rb <a. It will upload the file or project on github. git push origin master If you use -u in the command, it will remember your preferences for remote and branch and you can simply use the command git push next time.

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"

Little Known Ways To Utilize GitHub Gists, A gist can be a string of code, a bash script or some other small. You can also drag and drop a file onto your gist editor and it will be added Upload images to a gist — clone your gist as a git repository and add your picture,  To create a public gist, click Create public gist. To create a secret gist, click Create secret Gist . Note: After creating a gist, you cannot convert it from public to secret.

creating a gist from the command-line, Finally, if you did not want to put this code into its own file you could create a shell function instead. gist as a bash function. You have probably  ShellCheck suggests the following. 😄 Also, shameless plug, I'm the founder of https://commando.io, a web service that allows you to run scripts like this on servers (ssh) from a beautiful web-interface, on a schedule (crontab like), or via GitHub push.

python-gist · PyPI, Otherwise, you can use the config file to ensure that 'gist' uses the editor you want it to use. If the token string begins with ! the text following is interpreted as a shell​  Number of simultaneous uploads. If the script does not fully utilize your bandwidth, you can maybe speed things up a little bit by increasing the value of max_upload_threads. When you start the upload of more than one file, the script will start up to max_upload_threads parallel uploads, but only one thread per file.

What You Can Do With Gists on Github?, Github offers another useful service called Gist that developers often use to dump their code snippets but Gists aren't This can also be used for comparing text files. The app is available for Mac OSX, Windows and Linux. Bash and zsh alias for transfer.sh. Transfers files and directories to transfer.sh. - transfer.fish

Sending gist to github via cURL and issues with new lines inside file , = echo "WORLD" ?> And I wanted upload the contents of this file to my gists in github, which I am doing via gist_content  There are various good solutions for this for other environments (this question, for example, is very similar in scope but for bash, and the answers there cover bash, python, ruby etc Upload a file to a Gist with bash). I would appreciate pointers or solutions for this as in many searches elsewhere I have found no working methods to natively

Comments
  • Not a programming question, there's also a button that says "add file" on the page you link to.
  • 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
  • Gist is not the biggest platform for programmers, it's a feature of github, which is the a platform. Clearly you've not read the difference between what a github repository is and what a gist is, this is covered in the help documentation. 'Add File' get another input to paste your file contents into.
  • I clarified the purpose of my question, please reconsider your votes
  • related: How do you upload images to a gist?
  • @Elric Done !
  • I enhanced it a bit: gist.github.com/rubo77/3b8b15f4df5b5287a24f87523c41e4ce
  • 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 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 !
  • Why do you use gist for that? Lets move on with a repo that we can track its issues: github.com/ceremcem/create-gist
  • on Ubuntu: apt install gist gives the gem. I'm sure you can install with ruby if you prefer. works like a gem :)
  • Nice ;) But this doesn't explain how to do it with bash, is that possible?
  • @rubo77 yes: use the curl version I mention. It is a bash command.