Sub topic 1
How gh is organized and how to get help
Prerequisites
This chapter assumes you have the following:
- Basic Git usage: clone, commit, branch, and push.
- A GitHub account.
- Comfort with a shell such as bash or zsh.
- Familiarity with pull requests and issues as GitHub concepts.
You do not need to be logged in to gh yet. Authentication is the next chapter.
The command structure
gh is the GitHub command-line tool. Most commands follow one pattern:
gh <command> <subcommand> [flags]The first word names a command group, which is a set of commands about one kind of GitHub object. The second word is the action. Flags adjust the action.
| Command group | What it covers |
|---|---|
| `gh auth` | Logging in and checking accounts |
| `gh repo` | Cloning, creating, viewing, and forking repositories |
| `gh issue` | Creating and triaging issues |
| `gh pr` | Opening, checking out, reviewing, and merging pull requests |
| `gh run` | Watching GitHub Actions runs |
| `gh api` | Calling the REST and GraphQL APIs directly |
| `gh alias` | Defining shortcuts |
| `gh extension` | Installing extensions |
The same verbs recur across groups. list, view, create, and close work on issues and pull requests in the same way. After you learn one group, the others are easier to guess.
Getting help
gh has built-in help at every level. Use it before searching the web, because it matches the version you have installed.
gh --help
gh pr --help
gh pr create --helpThe first command lists all command groups. The second lists the actions in gh pr. The third lists every flag for gh pr create, with examples.
Two other commands are useful:
gh help reference
gh help formattinggh help reference prints the full command reference in one stream. gh help formatting explains the --json, --jq, and --template output options, which a later chapter covers.
Which repository does gh use?
Many commands act on a repository. If you run gh inside a Git clone, it reads the remotes and uses the matching GitHub repository. Outside a clone, you can name the repository with the -R (or --repo) flag:
gh issue list -R cli/cliYou can also set the GH_REPO environment variable, which the scripting chapter uses.
Common errors
unknown command "..." for "gh": You mistyped a group or action. Rungh --helporgh <group> --helpto check the names.none of the git remotes configured for this repository point to a known GitHub host: You ran a repository command in a directory that is not a GitHub clone. Move into a clone, or add-R owner/name.- A flag is rejected as unknown: Flags belong to a specific action. A flag valid for
gh pr listmay not exist forgh issue list. Checkgh <group> <action> --help. - A command asks questions you did not expect: In a terminal,
ghfalls back to interactive prompts when you omit required flags. Supplying the flags avoids the prompts.
Exercise
You want to see every flag available for creating an issue, and then list open issues in the repository cli/cli without cloning it. Which two commands do you run?
Show answer
Run gh issue create --help to see the flags. Run gh issue list -R cli/cli to list the issues. The -R flag names the repository, so no local clone is needed. Open issues are the default state.