How to Use GitHub CLI: Basic Commands and Examples
Use the
gh command followed by an action like repo, issue, or pr to interact with GitHub from your terminal. For example, gh repo clone owner/repo clones a repository, and gh pr create starts a pull request.Syntax
The basic syntax of GitHub CLI commands is gh [command] [subcommand] [flags]. Here:
- gh: The GitHub CLI tool.
- command: The main action like
repo,issue, orpr. - subcommand: Specific operation under the command, such as
clone,list, orcreate. - flags: Optional settings to customize the command behavior.
bash
gh [command] [subcommand] [flags]
Example
This example shows how to clone a repository, create a new issue, and open a pull request using GitHub CLI.
bash
gh repo clone cli/cli cd cli # Create a new issue gh issue create --title "Bug found" --body "There is a bug in the CLI tool." # Create a pull request gh pr create --title "Fix bug" --body "This PR fixes the bug." --base main --head fix-branch
Output
Cloning into 'cli'...
remote: Enumerating objects: 1000, done.
remote: Counting objects: 100% (1000/1000), done.
remote: Compressing objects: 100% (800/800), done.
remote: Total 1000 (delta 200), reused 900 (delta 150), pack-reused 0
Receiving objects: 100% (1000/1000), 2.5 MiB | 1.2 MiB/s, done.
Creating issue in cli/cli
Issue created: https://github.com/cli/cli/issues/1234
Creating pull request
Pull request created: https://github.com/cli/cli/pull/5678
Common Pitfalls
Common mistakes when using GitHub CLI include:
- Not authenticating with
gh auth loginbefore running commands that require permissions. - Using incorrect repository names or missing owner names in commands.
- Forgetting to specify the base and head branches when creating pull requests.
Always check your current authentication status with gh auth status.
bash
gh pr create --title "Fix bug" # Error: authentication required # Correct way: gh auth login gh pr create --title "Fix bug" --base main --head fix-branch
Output
Error: authentication required
# After login
Creating pull request
Pull request created: https://github.com/owner/repo/pull/1
Quick Reference
| Command | Description |
|---|---|
| gh auth login | Authenticate your GitHub account |
| gh repo clone owner/repo | Clone a GitHub repository |
| gh issue create --title | Create a new issue |
| gh pr create --title | Create a pull request |
| gh pr list | List open pull requests |
| gh issue list | List open issues |
Key Takeaways
Always authenticate first using
gh auth login to access private repositories and actions.Use
gh repo clone owner/repo to quickly clone repositories from the terminal.Create issues and pull requests directly with
gh issue create and gh pr create commands.Check your authentication status anytime with
gh auth status to avoid permission errors.Specify base and head branches clearly when creating pull requests to avoid mistakes.