GitHub CLI

GitHub CLI allows you to issue GitHub commands from your terminal.

Install

Install.

brew install gh

Update.

brew update && brew upgrade gh

Configure

Authorize the application to connect to GitHub.

gh auth login

Use HTTPS for operations.

gh config set git_protocol https
HTTPS vs. SSH

If you receive errors when issuing commands, log into gh again and explicitly select HTTPS as the protocol: gh auth login

Use

Commands

Refresh your authentication without logging in again.

gh auth refresh

Clone a repo.

gh repo clone "${owner_slash_repository}"

Set a default repo.

gh repo set-default "${owner_slash_repository}"
Select default repo

Run just gh repo set-default to show a list of repos to choose from.

Update the active repository's description.

gh repo edit --description "😁 My repo description"

Change the active repository's visibility, accepting the consequences.

gh repo edit --visibility public --accept-visibility-change-consequences

PRs

List all PRs, open or closed.

gh pr list --state all

List my recently closed PRs.

gh pr list --state closed --author @me --repo "${owner_slash_repository}" --limit 5

Using jq, list the branch names of my recently closed PRs.

gh pr list --state closed --author @me --repo "${owner_slash_repository}" --limit 5 --json headRefName | jq -r '.[] | .headRefName'

Get all mergeable PRs.

#!/bin/bash

REPO=${1}

# List all open PR numbers.
pr_numbers=$(gh pr list --repo "$REPO" --state open --json number --jq '.[].number')

# Loop through each PR and check mergeable status.
for pr_number in $pr_numbers; do
  pr_data=$(gh pr view $pr_number --repo "$REPO" --json mergeable,url,author,isDraft \
    --jq '{mergeable: .mergeable, url: .url, author: .author.login, isDraft: .isDraft}')
  mergeable_status=$(echo "$pr_data" | jq -r '.mergeable')
  pr_url=$(echo "$pr_data" | jq -r '.url')
  pr_author=$(echo "$pr_data" | jq -r '.author')
  is_draft=$(echo "$pr_data" | jq -r '.isDraft')

  if [ "$mergeable_status" == "MERGEABLE" ] && [ "$is_draft" == "false" ]; then
    echo "$pr_author - #$pr_number - $pr_url"
  fi
done

References

GitHub CLI
Interactive graph
On this page
Install
Configure
Use
Commands
PRs
References