Cheat Sheet - github-cli

Authenticate with GitHub Enterprise

gh auth login --hostname git.contoso.com --with-token < $(bw get password 'd8df5b3f-14e1-455d-88cb-b08100af4485')

Repo bulk operations

  • Note: This uses gh’s internal jq library, rather than a standalone jq.
  • Note: We move archived repos to a dedicated org to prevent our search results from getting cluttered up with old code.
# Bulk clone repos:
gh repo list $org --no-archived -L 500 --json sshUrl --jq '.[].sshUrl' | sort > repos.txt
cat repos.txt | xargs -I % git clone %

# Bulk unarchive repos:
gh repo list $org --archived -L 500 --json name --jq '.[].name' | sort > repos.txt
repos.txt | xargs -I % gh repo unarchive $org/% --yes

# Bulk archive repos:
# We use topics to cluster archived repos by scope and source org 
gh repo list $org --no-archived -L 500 --json name --jq '.[].name' | sort > repos.txt
cat repos.txt | xargs -I % gh repo edit $org/% --add-topic scope-foo --add-topic "src-org-$org"
cat repos.txt | xargs -I % gh api repos/$org/%/transfer -f new_owner="$org-archive"

Tip: Faster cloning for many repos

If you are cloning a large number of repos, you can speed up this script with GNU parallel.

gh repo list <ORG_NAME> --limit <LIMIT> --json sshUrl --jq '.[].sshUrl' | \
  parallel -j<JOBS> git clone

Alternatively you can also use xargs:

gh repo list <ORG_NAME> --limit <LIMIT> --json sshUrl --jq '.[].sshUrl' | \
  xargs -n 1 -P 8 git clone

Which breaks down like this:

  • taking at most one argument per run command line
  • and run up to eight processes at a time

JSON queries

# print only specific fields from the response
gh api repos/{owner}/{repo}/issues --jq '.[].title'

Disable pager

# Override the GH pager...
export GH_PAGER=cat

# ... or unset PAGER
PAGER= gh release view v1.0.3 --json name,isDraft

PRs

# Create a PR
gh pr create --title "feat: my_super_feature" --body "all the details"

# View the list of open pull requests
gh pr list

# Check CI for a PR
gh pr checks <PR_ID>

Workflow

# List workflow files in GitHub Actions
gh workflow list

# Run the workflow file 'build.yml' at the remote default branch.
gh workflow run build.yml

# Select a workflow to view interactively
gh workflow view

# Enable/Disable a workflow
gh workflow enable/disable build.yml

# List recent workflow runs
gh run list

# Select a run to view interactively
gh run view

# Watch a run until it completes
gh run watch 3456

# Cancel a workflow run
gh run cancel 3456

# Rerun a failed run
gh run rerun 3456

Misc.

gh repo edit --visibility <visibility-string>

# View Current GitHub repository in the web browser
gh browse

# Open repository settings
gh browse --settings

# Open issues or pull request
gh browse <ID>

# Open a commit page.
gh browse <COMMIT_ID>

# Open main.py file at line 340.
gh browse main.py:340