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 --webListing Your Gists
# Your gists
gh gist list
# Limit
gh gist list --limit 50
# Public only
gh gist list --public
# Secret only
gh gist list --secretViewing 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> --webEditing 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.pyCloning a Gist
Clone as a Git repository for local editing:
gh gist clone <id>
gh gist clone <id> target-directoryThen edit, commit, and push changes back with standard Git commands.
Renaming a Gist File
gh gist rename <id> old-name.sh new-name.shDeleting a Gist
gh gist delete <id>Exercises
- Create a gist:
echo "print('hello')" | gh gist create --filename hello.py --desc "Test" - List your gists:
gh gist list - View the gist:
gh gist view <id> - Edit it:
gh gist edit <id> - Delete it:
gh gist delete <id>