Skip to content

Instantly share code, notes, and snippets.

@ChristopherA
Last active December 27, 2022 15:34
Show Gist options
  • Save ChristopherA/3cca24936fb2c84786a29f67bacacd3e to your computer and use it in GitHub Desktop.
Save ChristopherA/3cca24936fb2c84786a29f67bacacd3e to your computer and use it in GitHub Desktop.
GitHub & `gh` Tips

Github & gh Tips

Tips & advice related to GitHub specific features of git, gpg, and development.

Table of Contents

General 'gh` Tips

install

  • macOS: brew install gh

First time auth with GitHub and create initial credentials and configuration

gh auth login

Set local script variable $GH_TOKEN to the gh token

GH_TOKEN=$(gh auth status -t 2>&1 | grep "Token:" | gsed -e 's/^.*: //g')
git credential reject <<<"url=https://github.com"

echo $GH_TOKEN

GitHub auth login using an existing token

gh auth login --with-token <<<$(cat $HOME/path/to/token.txt | head -1)

status

gh auth status -t

function to check login status in script

# hostname is necessary just in case you are logged into the CLI
# GitHub enterprise instance
if ! (gh auth status --hostname "github.com" > /dev/null 2>&1); then
  echo "Not logged into github".
  echo "Please run: gh auth login"
  exit 1
fi

logout

gh auth logout

Quickly create a GitHub repo (skip confirmation, private, turn off unecessary features)

gh repo create -y --private --enable-issues=false --enable-wiki=false -d "Description" REPO

Additional options:

  ORGANIZATION/REPO                  Specificy Organization (without ORGANIZATION/ will be personal)
  -h, --homepage URL          Repository home page URL
  -t, --team name             The name of the organization team to be granted access
  -p, --template repository   Make the new repository based on a template repository

list all personal repos (all, no forks, no archives, no private)

gh repo list --limit 1000 --source --no-archived --public 

Add gh repo list ORGANIZATION --limit 1000 … to list repos in organizaiton

Clone all your personal repos in one command (no forks, archived or public repos)

while read list_line; do read repo description visibility datetime <<< "$list_line" && gh repo clone "$repo"; done < <(gh repo list --source --no-archived --public | cat)

Clone all the repos in ORGANIZATION in one command (no forks, archived or public repos)

while read list_line; do read repo description visibility datetime <<< "$list_line" && gh repo clone "$repo"; done < <(gh repo list ORGANIZATION -source --non-archived --public | cat)
  • OR
#!/bin/bash
# clone-repos.sh

# Requires `gh` --> https://cli.github.com/

# Usage:
# $ ./clone-repos.sh some_org target_dir
# Cloning into 'target_dir/some_repo'...

gh repo list "$1" --limit 1000 -source --non-archived --public | while read -r repo _; do
  repo_name=$(echo "$repo" | grep -oP "^$1\K.*")
  gh repo clone "$repo" "$2$repo_name"
done
  • gh cloner function (to be rewritten)

(from https://gist.github.com/jaysonsantos/895c73fe011241b6e250c98f156ddb4f

function gclone() {
    local organization
    local repositories
    organization="$1"
    repositories="$(gh repo list -L 1000 "$organization" | fzf --multi --cycle | awk '{print $1}')"
    for repository in $repositories; do
        echo "Cloning $repository"
        gh repo clone "$repository"
    done
}

gh & PRs

CORE COMMANDS
  checkout:   Check out a pull request in git
  checks:     Show CI status for a single pull request
  close:      Close a pull request
  comment:    Create a new pr comment
  create:     Create a pull request
  diff:       View changes in a pull request
  edit:       Edit a pull request
  list:       List and filter pull requests in this repository
  merge:      Merge a pull request
  ready:      Mark a pull request as ready for review
  reopen:     Reopen a pull request
  review:     Add a review to a pull request
  status:     Show status of relevant pull requests
  view:       View a pull request
  
  A pull request can be supplied as argument in any of the following formats:
  - by number, e.g. "123";
  - by URL, e.g. "https://github.com/OWNER/REPO/pull/123"; or
  - by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1".

List PRs gh pr list

gh pr list
  -a @me          `# my prs` \
  --state closed  `# open/closed/all` \
  -w              `# show in webbrowser` \

More gh pr Examples

gh pr list --author @me
gh pr list --assignee @me
gh pr list --label bug --label "priority 1"
gh pr list --search "status:success review:required"
gh pr list --web

gh pr create --title "The bug is fixed" --body "Everything works again"
gh pr create --reviewer monalisa,hubot  --reviewer myorg/team-name
gh pr create --project "Roadmap"
gh pr create --base develop --head monalisa:feature

gh pr checkout 123
gh pr checkout 123 --force
gh pr checkout 123 --detach
gh pr checkout 123 --recurse-submodules

gh pr edit 123 --title "I found a bug" --body "Nothing works"
gh pr edit 123 --add-label "bug,help wanted" --remove-label "core"
gh pr edit 123 --add-reviewer monalisa,hubot  --remove-reviewer myorg/team-name
gh pr edit 123 --add-assignee @me --remove-assignee monalisa,hubot
gh pr edit1 23 --add-project "Roadmap" --remove-project v1,v2
gh pr edit 123 --milestone "Version 1"

gh pr merge 123 --merge --comment --body "merged old with new"
gh pr merge 123 --rebase
gh pr merge 123 --squash

gh pr ready 123

gh pr review --approve
gh pr review --comment --body "interesting"
gh pr review 123
gh pr review 123 --request-changes --body "needs more ASCII art"
gh pr review 123 --request-changes --body-file review.md

gh pr close 123 --delete-branch

Show changes in this PR

gh pr diff [<number> | <url> | <branch>] [flags]

Load PR (for instance #123)

gh pr checkout 123

Open current branch in GitHub

# For Bash or ZSH

function gh-pr() {
    branch=`git rev-parse --abbrev-ref HEAD`
    prNum=`gh pr list | grep '$branch' | grep -o '[0-9]\+' | head -1`
    gh pr view $prNum
}

Query Github PR

#!/bin/bash
set -e
if ! which gh 2>&1 > /dev/null; then
    echo 'Missing gh cli'
    exit 1
fi

if [ $# -eq 0 ]; then
    echo "usage: $0 <github query>"
    echo "Read more in https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests"
    exit 0
fi

# summary
gh pr list --search "$*"
echo "Relevant commands: view, diff, checks, comment, review, edit"
echo "Type <command> help for more info"
echo "Press C^D to go to next PR."
echo ""

set +e
# repl on each PR
for pr in $(gh pr list --search "$*" --json number -q ".[].number"); do 
    gh pr view --comments $pr
    while read -p "gh pr (#$pr)> " args; do gh pr $args $pr; done
    echo
done

gh gist tips

list all of your public gists

gh gist list --public

list private gists

gh gist list --secret

pick a gist from list in cli and view on web

clear; gh gist view --web

Quick create private gist

gh gist create mygisttopic.md

view a gist in markdown gist in raw mode

clear; gh gist view -r $(gh gist list --public | grep ".md" | cut  -f1 | head -1)

Other gh gist commands

  create:     Create a new gist
    -d, --desc string       A description for this gist
    -f, --filename string   Provide a filename to be used when reading from STDIN
    -p, --public            List the gist publicly (default: private)
    -w, --web               Open the web browser with created gist
  delete:     Delete a gist
  edit:       Edit one of your gists
    -a, --add string        Add a new file to the gist
    -f, --filename string   Select a file to edit
  list:       List your gists
    -L, --limit int   Maximum number of gists to fetch (default 10)
    --public      Show only public gists
    --secret      Show only secret gists
  view:       View a gist
    -f, --filename string   Display a single file from the gist
    --files             List file names from the gist
    -r, --raw               Print raw instead of rendered gist contents
    -w, --web               Open gist in the browser

Unsorted Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment