Shogo Oshima

—Web Developer

Sketches Blog About
GitHub X

GitHub CLI commands I use all the time

Git/GitHub

GitHub CLI lets you operate GitHub from the command line.

For example, you can create issues and pull requests, or check on them.

In this post, I’ll go through the GitHub CLI commands I find myself using the most.

This is only a small slice of what it can do, so see the documentation for the full picture. Installation instructions are in the docs as well.

gh browse

gh browse opens the current repository in the browser.

Even with GitHub CLI available, I’m still used to doing GitHub things in the browser, so I end up reaching for this command constantly.

Open the current repository’s home page

gh browse

Open the page for a specific directory

gh browse app

Open a specific issue page

gh browse 3

Open the page for a specific branch

gh browse -b dev

gh issue

gh issue covers issue-related operations.

Create an issue

Running the following lets you create an issue interactively:

gh issue create

After you run it, it asks you a series of questions like the ones below. You can also start in the CLI and continue in the browser partway through:

❯ gh issue create

Creating issue in osgsm/osgsm.io-v2

? Title (required) Add note for GitHub CLI
? Choose a template Open a blank issue
? Body <Received>
? What''s next?  [Use arrows to move, type to filter]
> Submit
  Submit as draft
  Continue in browser
  Add metadata
  Cancel

List pull requests

You can check pull requests with gh pr list:

❯ gh pr list

Showing 1 of 1 open pull request in osgsm/osgsm.io-v2

ID  TITLE                  BRANCH               CREATED AT
#4  Add "github-cli" post  add-post-github-cli  about 1 minute ago

Just like gh issue list, you can filter with options such as -a, or open the list in the browser with -w.

Check pull request status

Running gh pr status shows the pull requests relevant to you:

❯ gh pr status

Relevant pull requests in osgsm/osgsm.io-v2

Current branch
  #4  Add "github-cli" post [add-post-github-cli]
  ✓ Checks passing

Created by you
  #4  Add "github-cli" post [add-post-github-cli]
  ✓ Checks passing

Requesting a code review from you
  You have no pull requests to review

Check out a specific pull request

gh pr checkout <number> checks out a specific pull request. For example:

❯ gh pr checkout 12
remote: Enumerating objects: 66, done.
remote: Counting objects: 100% (66/66), done.
remote: Total 83 (delta 66), reused 66 (delta 66), pack-reused 17
Unpacking objects: 100% (83/83), done.
From https://github.com/owner/repo
 * [new ref]             refs/pull/8896/head -> patch-2
M       README.md
Switched to branch 'patch-2'

By the way, if you add the following function (introduced in this article on GitHub CLI tips, in Japanese) to your .zshrc, you can go from listing pull requests to checking one out with a single command. Thanks to the author 🙏🏻

prco () {
    gh pr list;
    echo "Type the number of PR to checkout: " && read number;
    gh pr checkout ${number};
}

Here’s what the function does:

  • Lists pull requests with gh pr list
  • Prompts you to type a pull request number
  • Assigns the input to number using read
  • Passes number to gh pr checkout and runs it

Run prco like this, then type the number you want to check out:

❯ prco

Showing 1 of 1 open pull request in osgsm/osgsm.io-v2

ID  TITLE                  BRANCH               CREATED AT
#4  Add "github-cli" post  add-post-github-cli  about 24 minutes ago
Type the number of PR to checkout:
4
Switched to branch 'add-post-github-cli'
Already up to date.

That’s all it takes to check out a specific pull request. Wonderful, isn’t it?

gh repo create

Last up is gh repo create, the command for creating repositories.

After starting a new project and running git init → git add → git commit locally, running gh repo create handles everything from creating the remote repository to pushing — all in one command.

Run gh repo create and answer the questions it asks:

❯ gh repo create

? What would you like to do? Push an existing local repository to GitHub
? Path to local repository .
? Repository name sample-project
? Repository owner osgsm
? Description
? Visibility Private
✓ Created repository osgsm/sample-project on GitHub
  https://github.com/osgsm/sample-project
? Add a remote? Yes
? What should the new remote be called? origin
✓ Added remote git@github.com:osgsm/sample-project.git
? Would you like to push commits from the current branch to "origin"? Yes
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 12 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (15/15), 9.61 KiB | 4.80 MiB/s, done.
Total 15 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:osgsm/sample-project.git
 * [new branch]      HEAD -> main
branch 'main' set up to track 'origin/main'.
✓ Pushed commits to git@github.com:osgsm/sample-project.git

And if you keep a function like this in your .zshrc, simply running ginit walks through git init → git add → git commit, then uses gh repo create to take you all the way to pushing to the remote repository:

ginit () {
    git init && git add . && git status && git commit -m "First commit" && git branch -M main;
    gh repo create;
}

Wrapping up

As you can see, GitHub CLI cuts down on browser trips and makes working with GitHub a lot easier.

Even if you’re firmly in the browser camp, gh browse alone makes GitHub CLI worth having — give it a try.

References