Clone a GitHub Gist with gh gist clone
Someone pasted a gist link in a ticket and you want the files on disk, with history, not copied out of a browser tab. gh gist clone turns that link into a proper Git checkout in about five minutes on a working connection. You need gh, git, and either a gist ID or its URL.
The route
Jump straight to the step you need, or tick off Done means at the end.
The installed command is GitHub CLI gh 2.87.3, and its package metadata on this machine also includes an older Ubuntu package entry. Verify the executable you are actually invoking before relying on version-specific behaviour.
- It downloads and creates a directory. That is all.
- It never edits the remote gist.
- The examples use an obvious placeholder. Replace it with a real ID before pressing Enter.
1. Check the executable and syntax
Confirm that the shell finds the expected binary and that the subcommand is installed:
$ command -v gh
/home/linuxbrew/.linuxbrew/bin/gh
$ gh --version
gh version 2.87.3 (2026-02-23)
$ gh gist clone --help
The help output shows the complete form:
gh gist clone <gist> [<directory>] [-- <gitflags>...]
Checkpoint
If gh gist clone --help fails, stop here. Check your PATH and installation rather than trying different flags from memory.
2. Pick a destination with no work in it
The optional directory sets the name and location of the checkout. Choose a fresh, explicit path. This check only reads the filesystem:
GIST_DIR="$HOME/src/example-gist"
if test -e "$GIST_DIR"; then
printf 'Refusing to use an existing path: %s\n' "$GIST_DIR" >&2
exit 1
fi
printf 'Destination is unused: %s\n' "$GIST_DIR"
3. Clone by gist ID
A gist ID is the long hexadecimal string in its URL. Put the ID first and the destination second:
$ GIST_ID='5b0e0062eb8e9654adad7bb1d81cc75f'
$ GIST_DIR="$HOME/src/example-gist"
$ gh gist clone "$GIST_ID" "$GIST_DIR"
Cloning into '/home/you/src/example-gist'...
remote: ...
Receiving objects: ...
Object counts and progress lines vary with the gist and your connection. A successful run leaves the directory as a Git working tree. Verify that without changing anything:
$ git -C "$GIST_DIR" rev-parse --is-inside-work-tree
true
$ git -C "$GIST_DIR" status --short
Checkpoint
An empty git status --short means the checkout has no local changes right now. It does not mean the gist is trustworthy. Read the files before running anything from an unfamiliar gist.
4. Clone by URL instead
The command also takes the full gist URL. Handy when you are copying a link from a ticket or browser:
$ GIST_URL='https://gist.github.com/OWNER/5b0e0062eb8e9654adad7bb1d81cc75f'
$ gh gist clone "$GIST_URL" "$HOME/src/example-gist"
Cloning into '/home/you/src/example-gist'...
remote: ...
- Replace both placeholders. That means
OWNERand the example ID. - Quote the URL. Otherwise shell punctuation can be read as part of your command.
- Both forms select the same object. Neither one executes the gist.
5. Pass Git options after the separator
Options meant for the underlying git clone go after a standalone --. For a shallow working copy:
$ gh gist clone "$GIST_ID" "$HOME/src/example-gist-shallow" -- --depth=1
Cloning into '/home/you/src/example-gist-shallow'...
remote: ...
Everything before the separator belongs to gh gist clone. Everything after it is forwarded to Git as a clone flag.
Warning
Keep the separator visible. Put --depth=1 before it and you change which program is expected to parse it.
Check the resulting history if shallow history matters:
$ git -C "$HOME/src/example-gist-shallow" rev-list --count HEAD
1
The count shows the expected shape, not a promise about every gist. A gist with a different history may report another value.
6. Fix the common failures
- Missing or mistyped ID. Git usually cannot find the remote object. Recheck the ID or copy the complete URL from the gist page.
- Gist is not public. Check that your
ghauthentication has access to it before retrying. Do not paste a token into the command line or into a gist URL. - Destination already exists. Choose another directory after inspecting it.
If a failed attempt created only disposable partial data, remove that exact directory after checking its path:
GIST_DIR="$HOME/src/example-gist"
case "$GIST_DIR" in
"$HOME"|"$HOME/"|"" )
printf '%s\n' 'Refusing to remove an unsafe path' >&2
exit 1
;;
esac
test -d "$GIST_DIR" && rm -rf -- "$GIST_DIR"
Warning
rm -rf is irreversible through the shell and deletes every file below the exact path. Use it only when you have confirmed the directory holds no work you need.
Recovery
There is no undo for a deleted local checkout. Reclone it if the remote gist is still available.
7. Inspect before editing or running
Once the clone succeeds, list its files and check the Git remote:
$ git -C "$GIST_DIR" ls-files
$ git -C "$GIST_DIR" remote -v
Warning
A successful clone does not make a gist safe. It can contain shell commands, credentials, or code with side effects. Read it first, avoid executing scripts as part of inspection, and keep secrets out of files you later add or edit.
Done means
- Syntax matched.
gh gist clone --helpagrees with the command you ran. - Real inputs used. You supplied a real gist ID or full gist URL and a fresh destination.
- Working tree confirmed.
git -C DIRECTORY rev-parse --is-inside-work-treereturnedtrue. - History known. You know whether the checkout has full or shallow history.
- Contents read. You inspected the files and remote before running unfamiliar content.