GitHub CLI (gh) – Tips and Guides

gh extension

 

A collection of useful commands and scripts for managing your GitHub repositories and workflow from the command line using the official GitHub CLI.

1. Authentication

Before you can use gh, you need to authenticate with your GitHub account.

# Start the interactive login process
gh auth login

Follow the prompts to authenticate via your web browser. It’s recommended to log in with SSH for the best experience.

2. Managing Repositories

Commands for creating, cloning, and managing repository settings.

Create a Repository

You can quickly create a new repository on GitHub.

# Create a new repository from the current directory
gh repo create

# Or, specify the name and other details
gh repo create my-new-project --public --source=. --remote=upstream

Clone Repositories

Clone a single repository or all repositories for a user.

# Clone a single repository
gh repo clone nbpacio/github-cli

# Clone all repositories for a specific user (including private)
# This script will clone them into folders named OWNER/REPO
gh repo list nbpacio --limit 4000 | while read -r repo _; do
  gh repo clone "$repo" "$repo"
done

Change Repository Visibility

Change a repository’s visibility between public and private.

# Change a specific repository to private
gh repo edit OWNER/REPO --visibility private

# Change a specific repository to public
gh repo edit OWNER/REPO --visibility public

Script: Make All Repositories Private

This script will iterate through all of a user’s repositories and set their visibility to private.

Warning: This is a destructive action. Use with caution.

#!/bin/bash

# Replace YOUR_USERNAME with your GitHub username
USERNAME="YOUR_USERNAME"

# Get a list of all repositories for the user
REPOS=$(gh repo list "$USERNAME" --limit 1000 --json name --jq '.[].name')

for repo in $REPOS; do
    echo "Setting $USERNAME/$repo to private..."
    # The command now implicitly accepts the consequences for non-interactive use
    gh repo edit "$USERNAME/$repo" --visibility private
    if [ $? -eq 0 ]; then
        echo "  -> Successfully set to private."
    else
        echo "  -> Error setting $USERNAME/$repo to private."
    fi
done

Set file as executable

chmod +x make_private.sh

Execute file

./make_private.sh

3. Working with Pull Requests

Streamline your pull request workflow.

List and Checkout Pull Requests

# List open pull requests in the current repository
gh pr list

# Checkout a pull request locally
gh pr checkout 123 # Checks out PR number 123

Create a Pull Request

# Create a pull request from your current branch
gh pr create --title "My Awesome Feature" --body "Here are the details."

# Create a draft pull request
gh pr create --draft

4. Working with Issues

Manage issues without leaving the command line.

# List open issues
gh issue list

# Create a new issue
gh issue create --title "Fix the bug" --body "Something is broken."

# View an issue with its comments
gh issue view 45

5. Aliases and Configuration

Speed up your workflow by creating aliases for your most-used commands.

# Create an alias 'co' for 'pr checkout'
gh alias set co 'pr checkout'

# Now you can use:
gh co 123

# List all your aliases
gh alias list

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top