Basic Commands Beginner

GitHub Copilot CLI has two core commands: gh copilot suggest for getting command suggestions and gh copilot explain for understanding commands. This lesson covers both in detail with practical examples.

gh copilot suggest

The suggest command takes a natural language description and returns the appropriate shell command. You can specify which type of command you want or let Copilot decide automatically.

Basic Syntax

Terminal
# Basic usage
$ gh copilot suggest "your description here"

# Specify command type with -t flag
$ gh copilot suggest -t shell "your description here"
$ gh copilot suggest -t git "your description here"
$ gh copilot suggest -t gh "your description here"

Command Types

There are three command types you can request:

Type Flag Description Example Commands
General Shell -t shell Operating system commands, file operations, text processing, networking find, grep, awk, curl, chmod
Git -t git Git version control commands for branching, committing, rebasing git rebase, git stash, git cherry-pick
GitHub CLI -t gh GitHub CLI commands for repositories, PRs, issues, Actions gh pr create, gh issue list, gh run view

Shell Command Examples

Terminal
# Find large files
$ gh copilot suggest -t shell "find all files larger than 50MB in current directory"
# Suggestion: find . -type f -size +50M

# Disk usage
$ gh copilot suggest -t shell "show disk usage of top-level directories sorted by size"
# Suggestion: du -h --max-depth=1 | sort -hr

# Process management
$ gh copilot suggest -t shell "find the process using port 3000 and kill it"
# Suggestion: lsof -ti :3000 | xargs kill -9

# Text processing
$ gh copilot suggest -t shell "count lines of code in all Python files recursively"
# Suggestion: find . -name '*.py' -exec wc -l {} + | tail -1

# File compression
$ gh copilot suggest -t shell "compress the logs directory into a tar.gz file"
# Suggestion: tar -czf logs.tar.gz logs/

Git Command Examples

Terminal
# Undo last commit
$ gh copilot suggest -t git "undo the last commit but keep changes staged"
# Suggestion: git reset --soft HEAD~1

# Interactive rebase
$ gh copilot suggest -t git "squash the last 3 commits into one"
# Suggestion: git rebase -i HEAD~3

# Branch management
$ gh copilot suggest -t git "delete all local branches that have been merged into main"
# Suggestion: git branch --merged main | grep -v 'main' | xargs git branch -d

# Stash operations
$ gh copilot suggest -t git "stash only the unstaged changes"
# Suggestion: git stash --keep-index

# View history
$ gh copilot suggest -t git "show commits by a specific author in the last week"
# Suggestion: git log --author="name" --since="1 week ago" --oneline

GitHub CLI Examples

Terminal
# Pull requests
$ gh copilot suggest -t gh "list all open pull requests assigned to me"
# Suggestion: gh pr list --assignee @me --state open

# Issues
$ gh copilot suggest -t gh "create a new issue with the bug label"
# Suggestion: gh issue create --label bug

# Actions
$ gh copilot suggest -t gh "view the status of the latest CI run"
# Suggestion: gh run list --limit 1

# Repository
$ gh copilot suggest -t gh "clone a repository and cd into it"
# Suggestion: gh repo clone owner/repo && cd repo

gh copilot explain

The explain command takes a command (or error message) and provides a clear, detailed explanation of what it does.

Basic Syntax

Terminal
$ gh copilot explain "command to explain"

Explanation Examples

Terminal
# Explain a complex find command
$ gh copilot explain "find . -name '*.log' -mtime +30 -exec rm {} \;"

# Explanation:
# This command finds and deletes log files older than 30 days:
#   find .              - Search in current directory and subdirectories
#   -name '*.log'       - Match files ending with .log
#   -mtime +30          - Modified more than 30 days ago
#   -exec rm {} \;      - Execute rm on each matching file
Terminal
# Explain a git command
$ gh copilot explain "git rebase -i --autosquash HEAD~5"

# Explanation:
# This command starts an interactive rebase of the last 5 commits:
#   git rebase          - Reapply commits on top of another base
#   -i                  - Interactive mode (lets you edit, squash, reorder)
#   --autosquash        - Automatically apply fixup!/squash! prefixed commits
#   HEAD~5              - Go back 5 commits from the current HEAD
Terminal
# Explain a networking command
$ gh copilot explain "ssh -L 8080:localhost:3000 user@server.com -N"

# Explanation:
# This command creates an SSH tunnel (port forwarding):
#   ssh                 - Secure Shell client
#   -L 8080:localhost:3000 - Forward local port 8080 to port 3000 on the remote
#   user@server.com     - Connect as 'user' to 'server.com'
#   -N                  - Don't execute a remote command (tunnel only)
Terminal
# Explain an error message
$ gh copilot explain "EACCES: permission denied, access '/usr/local/lib/node_modules'"

# Explanation:
# This Node.js error means you don't have write permission to the
# global node_modules directory. This usually happens when trying to
# install npm packages globally without proper permissions.
# Solutions:
#   1. Use sudo: sudo npm install -g package-name
#   2. Fix npm permissions (recommended): change the npm prefix
#   3. Use a version manager like nvm

Interactive Mode

When you run gh copilot suggest without a description, or after receiving a suggestion, you enter interactive mode. This allows you to refine your request and iterate on the result.

Terminal
$ gh copilot suggest

# ? What kind of command are you looking for?
#   > generic shell command
#     git command
#     gh command

# ? Describe what you'd like to do:
# > find duplicate files by content in a directory

# Suggestion:
# find . -type f -exec md5sum {} + | sort | uniq -w32 -dD

# ? Select an option:
#   > Copy command to clipboard
#     Execute command
#     Revise command
#     Rate response
#     Exit
Revise to Refine: If the suggested command is not quite right, choose "Revise command" to provide additional context. For example, if it suggested a Linux-specific command and you need a macOS-compatible one, you can ask it to revise for macOS.

Understanding Output and Safety

Copilot CLI provides safety features to help you understand and trust the commands it suggests.

Safety Warnings

When a suggested command could be destructive (e.g., deleting files, force-pushing), Copilot CLI displays a warning:

Terminal
$ gh copilot suggest "delete all files in the current directory"

# Suggestion: rm -rf ./*
# 
# ⚠ Warning: This command will permanently delete files.
# Review carefully before executing.
Always Review: Copilot CLI suggestions are AI-generated and may not always be correct. Always review commands before executing them, especially those that modify or delete data, change permissions, or interact with production systems.

Command Options After Suggestion

After receiving a suggestion, you have several options:

Option Description When to Use
Copy command to clipboard Copies the command so you can paste and review it When you want to modify the command before running
Execute command Runs the command directly in your terminal When you trust the suggestion and want to run it immediately
Revise command Provide additional context to refine the suggestion When the suggestion is close but not exactly what you need
Rate response Provide feedback to help improve future suggestions When you want to help improve the AI
Exit Close the suggestion session When you want to cancel

Quick Reference

Terminal
# Get help
$ gh copilot --help
$ gh copilot suggest --help
$ gh copilot explain --help

# Suggest with type
$ gh copilot suggest -t shell "description"
$ gh copilot suggest -t git "description"
$ gh copilot suggest -t gh "description"

# Explain a command
$ gh copilot explain "command here"

# Interactive mode (no arguments)
$ gh copilot suggest

Try It Yourself

Practice using both commands:

  1. Use gh copilot suggest -t shell to find a command for listing all open network ports
  2. Use gh copilot suggest -t git to find a command for viewing the diff of a specific commit
  3. Use gh copilot suggest -t gh to find a command for listing your repositories
  4. Use gh copilot explain to understand this command: tar -xzf archive.tar.gz -C /tmp/
  5. Try interactive mode by running gh copilot suggest with no arguments