You’ve probably heard of GitHub, the platform for hosting, storing, and editing code. Many open-source projects and private development teams use this web app to share their work.

But did you know that GitHub has a well-hidden spin-off called GitHub Gist? It’s a faster, simpler way of sharing code online. Like Pastebin, Gist is a tool for sharing text over the internet. But it offers additional features, and it's notably backed by Git’s robust version control.

How to Find the GitHub Gist Website

GitHub Gist is the name of the site that hosts gists. A "gist" is a snippet of code that can either be public or secret.

The main GitHub site doesn’t particularly promote GitHub Gist, so you’ll need to either search for it or bookmark the URL for regular use.

The site is a subdomain of the main GitHub site and your login works across the two sites. Anyone can view public gists, but you’ll need to log in to create new gist content.

Creating a Gist

When you’re logged in to GitHub, the Gist home page displays a form allowing you to quickly create a new gist.

A screenshot of the main GitHub Gist page with a form for creating a new gist

Choose an appropriate filename, then enter the file’s content and click the Create gist button. You can configure the basic editor with a handful of formatting options. You can also choose whether the gist is secret or public via the Create button.

The editor will apply syntax-highlighting according to your file’s extension. Make sure you choose an appropriate extension for the file’s type. You’ll also see a nice type-specific preview when viewing certain files in a gist. Markdown files work particularly well for viewing and editing.

A screenshot of the GitHub Gist website showing a preview of a markdown file

When editing, you can add more files with the Add file button. Many gists are small enough to require just one file, but you can use more if necessary.

Working With Gists

If you click the Edit button, you’ll get one basic text editor per file. Gist allows you to make edits via its web interface, and for small code samples, this is perfectly sufficient.

However, behind the scenes, each gist is a Git repository. This means you can track file revisions and perform other git operations. The Revisions tab near the top left displays commits to your gist’s repository.

A screenshot of the GitHub Gist site showing revisions of a gist

Behind the Embed button are options to clone the repository, so you can work with a gist remotely. However, you should be aware that gist repositories are slightly restricted. In particular, although a gist can contain many files, it cannot contain directories.

Gists on the Command Line

The GitHub command-line program, gh, has support for gists. You can create, delete, list, and edit gists directly from a terminal. This is more convenient than treating the gist like a normal git repository.

For example, if you have an existing file, you can immediately create a secret gist from it using this command:

        $ gh gist create index.md
- Creating gist index.md
✓ Created gist index.md
https://gist.github.com/027442d9e34f35ee4bf64bbbc1a81a62

The command finishes with a URL representing the new gist. You can also edit a gist:

        gh gist edit 027442d9e34f35ee4bf64bbbc1a81a62
    

This will open up your default text editor. Once you’ve made an edit and closed your editor, gh pushes your change to GitHub automatically.

How to Reuse Gist Content

When viewing a gist, you’ll notice a button named Raw alongside each file. This provides a plain text version of the file which can be useful for saving or copying. Note that you cannot use this to host files. For example, if you try to reference a JavaScript file using that link, it won’t work.

You can, however, display the contents of a gist elsewhere. This is great for code samples, and the syntax highlighting means they’ll look great in blog posts or articles. To do this, copy the code next to the Embed button and include it in your HTML.

A screenshot of an example embedded gist

Gists Are an Unsung Utility

GitHub doesn’t advertise gists very much, but don’t let that put you off. They’re a very accessible combination of Pastebin and Git. Gists are a great use case for any small code snippet that doesn’t warrant a full-blown repository. They make for an excellent lightweight option if you just want to quickly share some code. The embed option can come in handy if you’re publishing a technical article.

Check out other apps that allow you to share code-snippets with our guide to Pastebin alternatives.