Sub topic 14
Extending gh with extensions
This chapter assumes you can run gh commands, are signed in with gh auth login, and have read the chapters on --json/--jq output and scripting. You will install an extension, manage it, and write a small one yourself.
What an extension is
An extension is a GitHub repository whose name starts with gh-. It provides an executable that gh runs as a subcommand. If you install dlvhdr/gh-dash, the command becomes gh dash. The gh- prefix is dropped when you invoke it.
Extensions come in two kinds:
- Script extensions are an executable file in the repository root, such as a bash script. The file has the same name as the repository.
- Precompiled extensions ship binaries as release assets, one per operating system and architecture. Go is the most common language for these.
Extensions cannot override built-in commands. If an extension shares a name with a core command such as pr, the core command wins.
Installing and managing extensions
Install by giving the owner/repo name:
gh extension install dlvhdr/gh-dash
gh extension list
gh dashOther management commands:
gh extension search dashboard
gh extension upgrade --all
gh extension remove dashgh extension search finds repositories tagged with the gh-extension topic. gh extension browse offers an interactive version of the same search. To stay on a known version, install with --pin, for example gh extension install dlvhdr/gh-dash --pin v4.0.0. A pinned extension is skipped by upgrade.
Writing a script extension
gh extension create scaffolds a repository for you. The following example builds an extension that prints how many pull requests are open in the current repository.
gh extension create prcount
cat > gh-prcount/gh-prcount <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
gh pr list --state open --json number --jq 'length'
EOF
chmod +x gh-prcount/gh-prcount
cd gh-prcount
gh extension install .
gh prcountThe executable is named gh-prcount, matching the directory, which is how gh finds it. Installing with . links the local directory, so edits take effect immediately. The script calls gh itself and uses --json and --jq, so it reuses your existing authentication. It also runs in whatever repository you are standing in.
To share it, push the repository to GitHub and add the gh-extension topic so that gh extension search can find it:
git add .
git commit -m "Add prcount"
gh repo create gh-prcount --public --source=. --push
gh repo edit --add-topic gh-extensionOthers can then run gh extension install YOUR_USER/gh-prcount.
Common errors
| Symptom | Likely cause |
|---|---|
| `gh: unknown command "x"` | The extension is not installed, or the repository name does not start with `gh-`. |
| Permission denied when running | The script lacks the executable bit. Run `chmod +x`. |
| Local edits not picked up | The extension was installed from GitHub, not with `gh extension install .`. |
| Precompiled extension fails to install | No release asset matches your operating system and architecture. |
| Extension stays on an old version | It was installed with `--pin`, so `upgrade` skips it. |
Exercise
You wrote an executable script named gh-hello in a repository folder called gh-hello. Which command installs it locally from inside that folder, and how do you run it afterward?
Show answer
Run gh extension install . to install it from the current directory, then run gh hello. The gh- prefix is dropped when you invoke the extension.
Exercise
A teammate says their extension named gh-pr does not replace gh pr. Why?
Show answer
Extensions cannot override built-in commands. When a name matches a core command, gh runs the core command.