The GitHub CLI (gh) lets you manage repositories, issues, pull requests, and more directly from your terminal without opening a browser.

What You'll Need

  • A GitHub account
  • Git installed (version 2.0 or later)
  • Terminal access (Command Prompt, PowerShell, Terminal, or similar)
  • Internet connection for authentication and API calls

Step-by-Step

  1. Install the GitHub CLI: On Windows, use winget install GitHub.cli; on macOS, use brew install gh; on Linux, use sudo apt install gh (Debian/Ubuntu) or sudo dnf install gh (Fedora).
  2. Open your terminal and authenticate with GitHub by running gh auth login. Choose HTTPS as the protocol, select GitHub.com, and follow the prompts to paste the code into your browser and authorize the device.
  3. Verify authentication succeeded by running gh auth status. You should see your username and scopes listed.
  4. Clone a repository using gh repo clone owner/repo. Replace owner/repo with the actual repository path (e.g., gh repo clone octocat/Hello-World).
  5. Navigate into the cloned directory with cd repo-name and list branches using gh branch list.
  6. Create a new branch with gh branch create new-feature and switch to it using gh branch checkout new-feature.
  7. Make changes to files locally, then stage them with git add . and commit with git commit -m "Add feature".
  8. Push the branch to GitHub using git push origin new-feature.
  9. Open a pull request with gh pr create --title "Add new feature" --body "This adds the requested functionality" --base main --head new-feature.
  10. View your pull requests with gh pr list and check their status.
  11. Merge an approved pull request using gh pr merge 123 --merge --delete-branch, replacing 123 with the PR number.
  12. Delete a remote branch locally with gh branch delete -r origin/old-branch after it has been merged.

Common Mistakes

  • Forgetting to authenticate before running commands, resulting in "Not logged in" errors.
  • Using SSH URL format in gh repo clone when HTTPS authentication was set up—use owner/repo format instead.
  • Attempting to create a PR without pushing the branch first, which causes the command to fail silently or with upstream errors.
  • Using git push without setting upstream, leading to "No upstream branch" messages—use git push -u origin branch or rely on gh to set it during PR creation.
  • Running gh pr merge on a draft PR, which fails unless the PR is marked ready for review.

Tips

  • Use gh repo view --web to open the repository in your browser instantly.
  • Set a default editor for commit messages and PR bodies with gh config set editor "code --wait" (for VS Code).
  • Enable command completion: add eval "$(gh completion -s bash)" to your ~/.bashrc or ~/.zshrc.
  • Use gh issue create --label "bug" --assignee @me to quickly file a bug and assign it to yourself.
  • Check your rate limit status with gh api rate_limit to avoid hitting API limits during automation.
  • Alias frequent commands: gh alias set prl "pr list --limit 10" creates a shortcut gh prl.

Troubleshooting

  • If gh auth login fails to open the browser, manually copy the verification code and go to to enter it.
  • For "401 Unauthorized" errors, re-run gh auth login to refresh your token, as tokens expire or may be revoked.
  • If gh repo clone says "repository not found", double-check the spelling and ensure you have access—private repos require proper authentication.
  • When gh pr create fails with "no pushed commits", ensure you ran git push before creating the PR.
  • For slow performance, update gh with gh upgrade to get the latest bug fixes and performance improvements.
  • If completion or aliases don’t work, restart your terminal after adding them to your shell profile.

Frequently Asked Questions

Can I use gh with GitHub Enterprise?

Yes, use gh auth login --hostname your-enterprise.com to authenticate with a self-hosted GitHub Enterprise instance.

Does gh replace git?

No, gh works alongside git—it handles GitHub-specific actions like PRs and issues, while git manages source control.

Is gh available on Windows?

Yes, it is officially supported on Windows via Winget, MSI installer, or Chocolatey.

How do I update gh?

Run gh upgrade to check for and install the latest version.

Can I use gh in scripts?

Yes, it is designed for scripting—use --json flags with commands like gh pr list --json number,title for parseable output.

Does gh work without Git?

No, gh requires Git to be installed and accessible in your PATH for underlying operations like cloning and pushing.