Gists are lightweight, version-controlled code snippets hosted on GitHub. The gh gist command lets you create and manage them without visiting gist.github.com.

Creating a Gist

# From a file
gh gist create script.sh
 
# Multiple files
gh gist create file1.py file2.py file3.py
 
# With a description
gh gist create script.sh --desc "Useful backup script"
 
# Public gist (default is secret)
gh gist create script.sh --public
 
# From stdin
echo "Hello, World!" | gh gist create --filename hello.txt
 
# Open in browser after creating
gh gist create script.sh --web

Listing Your Gists

# Your gists
gh gist list
 
# Limit
gh gist list --limit 50
 
# Public only
gh gist list --public
 
# Secret only
gh gist list --secret

Viewing a Gist

# View contents in terminal
gh gist view <id-or-url>
 
# View a specific file within a multi-file gist
gh gist view <id> --filename script.sh
 
# View raw content (no syntax highlighting)
gh gist view <id> --raw
 
# Open in browser
gh gist view <id> --web

Editing a Gist

# Open in your editor
gh gist edit <id>
 
# Edit a specific file
gh gist edit <id> --filename script.sh
 
# Add a new file to an existing gist
gh gist edit <id> --add newfile.py

Cloning a Gist

Clone as a Git repository for local editing:

gh gist clone <id>
gh gist clone <id> target-directory

Then edit, commit, and push changes back with standard Git commands.

Renaming a Gist File

gh gist rename <id> old-name.sh new-name.sh

Deleting a Gist

gh gist delete <id>

Exercises

  1. Create a gist: echo "print('hello')" | gh gist create --filename hello.py --desc "Test"
  2. List your gists: gh gist list
  3. View the gist: gh gist view <id>
  4. Edit it: gh gist edit <id>
  5. Delete it: gh gist delete <id>