Phase 4GitHub

#15 GitHub CLI

gh repo, gh pr, gh issue

What is gh?

gh is the official GitHub command-line tool. It lets you do from the terminal everything you would normally do on the GitHub website: create repositories, manage pull requests, issues, and much more.

gh auth -- Authenticate

Before using gh, you need to log in to your GitHub account. Use gh auth login to start the authentication process, then verify with gh auth status:

Check authentication
$ gh auth status
github.com
  ✓ Logged in to github.com account alice (keyring)
  - Active account: true
  - Git operations protocol: https
  - Token: gho_****
  - Token scopes: gist, read:org, repo, workflow

gh repo -- Manage repositories

Create a new repository directly from the terminal with gh repo create:

Create a repository
$ gh repo create my-app --public
✓ Created repository alice/my-app on GitHub
  https://github.com/alice/my-app

Clone an existing repository with gh repo clone:

Clone a repository
$ gh repo clone alice/my-app
Cloning into 'my-app'...
done.

gh pr -- Manage Pull Requests

Create a pull request from your current branch:

Create a PR
$ gh pr create --title "Add feature" --body "Description"
Creating pull request for feature into main in alice/my-project

https://github.com/alice/my-project/pull/1

List open PRs with gh pr list:

List PRs
$ gh pr list
Showing 2 of 2 open pull requests in alice/my-project

#2  Fix bug      fix-bug     OPEN
#1  Add feature  feature     OPEN

You can also use gh pr view to see PR details and gh pr merge to merge it.

gh issue -- Manage issues

Issues are used to report bugs or request features. Create one from the terminal:

Create an issue
$ gh issue create --title "Bug report" --body "Details"
Creating issue in alice/my-project

https://github.com/alice/my-project/issues/1

List open issues:

List issues
$ gh issue list
Showing 1 of 1 open issue in alice/my-project

#1  Bug report  OPEN  about 1 minute ago

gh workflow -- Manage workflows

GitHub Actions lets you automate tasks (tests, deployment, etc.). With gh workflow, you can list and trigger these workflows:

List workflows
$ gh workflow list
Deploy   deploy.yml   active
Tests    tests.yml    active
Trigger a workflow
$ gh workflow run tests.yml
✓ Created workflow_dispatch event for tests.yml

To see runs for this workflow, try: gh run list --workflow=tests.yml

Summary

Overview
gh auth login                              # Log in
gh auth status                             # Check connection
gh repo create name --public               # Create a repository
gh repo clone user/repo                    # Clone a repository
gh pr create --title "..." --body "..."    # Create a PR
gh pr list                                 # List PRs
gh issue create --title "..." --body "..." # Create an issue
gh issue list                              # List issues
gh workflow list                           # List workflows
gh workflow run file.yml                   # Trigger a workflow

Your turn

Try the GitHub CLI commands in the terminal below. Start with gh auth status to check your connection.

terminal — bash
user@stemlegacy:~/my-project$