35

I am trying to create a gist from bash, and I have tried many versions scripts I could get, but none are working.

This seems like a correct one, but it does not work also.

curl -X POST -d '{"public":true,"files":{"test.txt":{"content":"String file contents"}}}' https://api.github.com/gists

I have a test.txt file with content I would like to create the gist, but it won't work. It says, invalid email, if I try to add -u USER or -u USER:PASS it still won't work saying "message": "Problems parsing JSON", ..

I don't know what is wrong. the documentation does not provide much except for this line:

POST /gists as you can see, I am passing the test.txt file.

7 Answers 7

34

Recently GitHub CLI released. So you can now use it instead.

Just install it to your system (https://github.com/cli/cli#installation)

The authenticate (it's pretty simple)

gh auth login

After you logged in you can simply create a new gist by:

gh gist create -d "my test gist" -f some_local_file.txt  test_gist

For more details you can use help:

gh <command> <subcommand> --help
Sign up to request clarification or add additional context in comments.

5 Comments

To use it on NixOS or with the Nix package manager, use nix-shell -p github-cli.
@Nickolay Kondratenko I am trying to push new local file into gist but its giving me an error failed to collect files for posting: failed to read file test_gist: open test_gist: no such file or directory gh gist create -d "Snwflake-Queries" -f Snflk_Query.sql test_gist
@Karthik that's because test_gist file doesn't exist. Your command should be the following gh gist create -d "Snwflake-Queries" Snflk_Query.sql
@Karthik from help: -f, --filename string Provide a filename to be used when reading from STDIN
@NickolayKondratenko Thanks a lot . Whats the command to overwrite the same file with new changes
15

I've just tried

curl -X POST -d '{"public":true,"files":{"test.txt":{"content":"String file contents"}}}' -u mgarciaisaia:mypassword https://api.github.com/gists

and it worked: https://gist.github.com/mgarciaisaia/fa51238073eb2cf508aa

I don't see any problem in your command.

6 Comments

btw: I have been trying to replace the test.txt and the String file.. with variables, but being inside single quotes, it does not work. Do you have any idea how to pass a variable inside single quotes?
But, how can we gist file.cpp snippet using that?
Now it says "message": "Not Found", "documentation_url": "developer.github.com/v3"
stackoverflow.com/a/64025613/9130688 response is valid for 2020
@DimitriKopriwa you'd need to read the file's content into a variable, and include that in the payload. That would imply changing the quotes and escaping, too. Maybe there's a new endpoint now?
|
15

This question is old, so I am not sure it is still relevant.

On Ubuntu (at least on 18.04), you can try the gist package, which will install the gist-paste command that you can use (given you have a git account already) as below:

1) Get a gist OAuth2 token (it will create a ~/.gist file with the token). You have to do it once only:

$ gist-paste --login

Then, you can send your files, for instance:

$ gist-paste your-file.txt
$ cat .emacs.d/init.el | gist-paste -t el

There are many options: You can send file type/description (as above second example), delete gists, open gist in browser, etc... See gist-paste(1) or try gist-paste --help.

If you already have a gist token, you don't need to run gist-paste --login, just copy your ~/.gitconfig's oauth-token to ~/.gist. For example, if you have in ~/.gitconfig:

[github]
    oauth-token = foobar123

Just create a ~/.gist file with one line containing "foobar123".

[Edit] If your distribution does not provide the package, the project page is: https://github.com/defunkt/gist

1 Comment

Confirmed to work on Ubuntu 16.04 but I just did gist finename.txt
4

I've just started using http://defunkt.io/gist, it makes things pretty simple:

# upload string to file
gist -f test.txt <<< "hello string"

# upload a file
gist test.txt

2 Comments

This is amazing! To install it use sudo gem install gist and then gist --login and put the 8 digit code into github.com. To write multiple lines just type gist and exit after several lines with CTRL-D.
Or, if one is on NixOS or using Nix as a package manager, use nix-shell -p gist.
3

Have the same desire I found https://www.npmjs.com/package/gistup and fork the repository to https://github.com/CrandellWS/mkg because the developer did not want to support Windows which was the operating system being used at the time. So I reworked the npm package to work on windows as well as linux and apple...

Full source is available on GitHub: https://github.com/CrandellWS/mkg

Installation is simple with npm

npm install -g mkg

Use is discribed on the npmjs package page: https://www.npmjs.com/package/gistup

Once installed simply cd to which every directory you want to make a gist from...(remeber there are no subfolders with Gists)

and run the command:

mkg

and it will open your new gist in a broswer...additionally you will be able to control it like a normal git from there... just no subfolders...

Comments

3

As of September 2020, creating gists is one of the features of GitHub's official command line tool gh.

E.g., on MacOS:

brew install gh
gh auth login  # Follow steps.
gh gist create myfile.txt  # Creates a private gist.

More options are available. gh gist create --help gives:

Create a new GitHub gist with given contents.

Gists can be created from one or multiple files. Alternatively, pass "-" as
file name to read from standard input.

By default, gists are private; use '--public' to make publicly listed ones.


USAGE
  gh gist create [<filename>... | -] [flags]

FLAGS
  -d, --desc string       A description for this gist
  -f, --filename string   Provide a filename to be used when reading from STDIN
  -p, --public            List the gist publicly (default: private)

INHERITED FLAGS
  --help   Show help for command

EXAMPLES
  # publish file 'hello.py' as a public gist
  $ gh gist create --public hello.py

  # create a gist with a description
  $ gh gist create hello.py -d "my Hello-World program in Python"

  # create a gist containing several files
  $ gh gist create hello.py world.py cool.txt

  # read from standard input to create a gist
  $ gh gist create -

  # create a gist from output piped from another command
  $ cat cool.txt | gh gist create

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual

2 Comments

If i want to udpate the existing file in the gist , whats the command using gh
Use gh gist edit {<gist ID> | <gist URL>} -f <filename>
0

You didn't post your system info so I'm using mine.

Install:

install: winget install --id GitHub.cli

Note: you must launch a new cli or close and restart your cli after installing so that your cli knows that the path is changed.

upgrade: winget upgrade --id GitHub.cli

public: gh gist create --public hello.py

private:gh gist create hello.py

You'll get a url, like this: Simple and effective, the url is like 4dad2955bffb6443792b385a972063c

1 Comment

Please see Why should I not upload images of code/data/errors?. That image could be a copy-paste of terminal text.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.