Copilot in the CLI
Use GitHub Copilot directly in your terminal to get intelligent command suggestions, understand complex commands, and accelerate your command-line workflow.
What is GitHub Copilot in the CLI?
GitHub Copilot in the CLI brings AI assistance to your terminal. Instead of searching Stack Overflow or man pages for the right command syntax, you describe what you want to do in plain English and Copilot suggests the exact command. It works as an extension to the GitHub CLI (gh) and supports three categories of commands: general shell commands, git commands, and gh CLI commands.
Copilot in the CLI is especially valuable for:
- Complex command syntax - Commands with many flags, pipes, and options that are hard to remember
- Infrequent operations - Tasks you do rarely enough that you forget the exact syntax each time
- Learning new tools - Discovering how to use commands you have never used before
- Cross-platform differences - Getting the right syntax for your current OS (macOS, Linux, Windows)
- Understanding existing scripts - Explaining what a complex command or pipeline does
Installation and Setup
Copilot in the CLI requires the GitHub CLI (gh) to be installed and authenticated. Here is how to set it up:
Step 1: Install the GitHub CLI
If you do not already have the GitHub CLI installed, install it for your platform:
# macOS (Homebrew) brew install gh # Windows (winget) winget install --id GitHub.cli # Ubuntu/Debian sudo apt install gh # Fedora sudo dnf install gh
Step 2: Authenticate with GitHub
# Log in to your GitHub account gh auth login # Verify authentication gh auth status
Step 3: Install the Copilot CLI Extension
# Install the Copilot extension gh extension install github/gh-copilot # Verify installation gh copilot --version
gh copilot suggest: Get Command Suggestions
The gh copilot suggest command is your primary tool for getting command-line help. Describe what you want to do in natural language, and Copilot returns the exact command with proper syntax and flags.
# Basic usage
gh copilot suggest "find all JavaScript files modified in the last 7 days"
# Copilot suggests:
# find . -name "*.js" -mtime -7
# More complex example
gh copilot suggest "compress all PNG files in current directory, keeping originals"
# Copilot suggests:
# for f in *.png; do cp "$f" "${f%.png}_original.png" && pngquant --force --output "$f" "$f"; doneWhen Copilot returns a suggestion, you have three options:
- Copy to clipboard - Copy the command to paste and potentially modify before running
- Execute - Run the command directly (Copilot asks for confirmation first)
- Revise - Refine your request with additional context for a better suggestion
You can also specify the type of command you need by selecting from the interactive prompt: generic shell command, git command, or gh command. This helps Copilot give more targeted suggestions.
Real-World Suggest Examples
# Git operations gh copilot suggest "undo the last 3 commits but keep the changes" # git reset --soft HEAD~3 gh copilot suggest "show all branches that have been merged into main" # git branch --merged main gh copilot suggest "find which commit introduced a bug in the login function" # git bisect start && git bisect bad HEAD && git bisect good v1.0 # System administration gh copilot suggest "show disk usage of current directory sorted by size" # du -sh * | sort -rh gh copilot suggest "find processes using port 3000 and kill them" # lsof -ti:3000 | xargs kill -9 gh copilot suggest "create a tar archive excluding node_modules and .git" # tar czf archive.tar.gz --exclude='node_modules' --exclude='.git' . # GitHub CLI operations gh copilot suggest "list all open PRs assigned to me" # gh pr list --assignee @me --state open gh copilot suggest "create an issue with labels bug and priority-high" # gh issue create --label bug --label priority-high
gh copilot explain: Understand Complex Commands
The gh copilot explain command does the reverse of suggest - you provide a command, and Copilot breaks it down into a human-readable explanation. This is invaluable when you encounter unfamiliar commands in scripts, documentation, or colleagues' code.
# Explain a complex git command gh copilot explain "git log --oneline --graph --all --decorate" # Copilot explains: # This command displays the git commit history with: # --oneline: Each commit on a single line (abbreviated hash + message) # --graph: ASCII art showing branch/merge structure # --all: Show commits from all branches, not just current # --decorate: Show branch names and tags next to commit hashes
# Explain a pipeline you found in a script
gh copilot explain "awk -F: '$3 >= 1000 {print $1}' /etc/passwd"
# Copilot explains:
# This command reads the /etc/passwd file and:
# -F: sets the field delimiter to colon (:)
# $3 >= 1000 filters for lines where the third field (UID) is 1000 or greater
# {print $1} outputs the first field (username) for matching lines
# Result: Lists all regular user accounts (non-system users)gh copilot explain before running unfamiliar commands from the internet. Understanding what a command does before executing it is a critical security practice, especially for commands with sudo, rm, or piped downloads.Tips for Writing Effective Queries
The quality of Copilot's suggestions depends heavily on how you phrase your request. Here are proven strategies for getting better results:
- Be specific about the tool - Say "using docker" or "with kubectl" to target a specific CLI tool
- Include constraints - Mention "without deleting originals," "recursively," or "only for .ts files" to narrow the output
- Specify the OS - If a command differs across platforms, mention "on macOS" or "on Ubuntu"
- Describe the desired output - "Show only the file name and size" is better than "list files"
- Use the revise option - If the first suggestion is close but not right, revise with additional details rather than starting over
| Weak Query | Strong Query | Why It's Better |
|---|---|---|
| "delete files" | "delete all .log files older than 30 days in /var/log" | Specifies file type, age, and location |
| "docker stuff" | "remove all stopped docker containers and unused images" | Specifies the exact cleanup operation |
| "git history" | "show git commits from last week by author john@example.com" | Specifies time range and author filter |
| "network check" | "test if port 443 is open on server api.example.com" | Specifies port number and target host |
Integrating Copilot CLI into Your Daily Workflow
To get the most out of Copilot in the CLI, integrate it into your regular terminal habits. Here are practical ways to make it part of your daily workflow:
Shell Aliases for Quick Access
Create short aliases so you can access Copilot commands faster:
# Add to your ~/.bashrc, ~/.zshrc, or shell config alias '??'='gh copilot suggest' alias '?!'='gh copilot explain' # Now you can use: ?? "find large files over 100MB in this repo" ?! "tar xzf archive.tar.gz -C /opt --strip-components=1"
Common Workflow Scenarios
Here are everyday situations where Copilot CLI saves significant time:
- Morning standup prep -
?? "show my git commits from yesterday across all branches" - Debugging deployments -
?? "show kubernetes pods in error state with their logs" - Database operations -
?? "dump postgres database mydb to a compressed file" - Log analysis -
?? "count unique IP addresses in nginx access log from today" - Cleanup tasks -
?? "remove all git branches that have been merged except main and develop"
Combining with Other Tools
Copilot CLI works alongside your existing terminal tools. You can use it to generate commands that feed into scripts, CI pipelines, or Makefiles:
# Use suggest to build a Makefile target gh copilot suggest "run eslint on all TypeScript files, fix auto-fixable issues, output results as JSON" # eslint --ext .ts,.tsx --fix --format json . # Use explain to document existing Makefile commands gh copilot explain "docker buildx build --platform linux/amd64,linux/arm64 -t myapp:latest --push ." # Multi-architecture Docker build targeting AMD64 and ARM64, # tagged as myapp:latest, automatically pushed to registry
rm, sudo, chmod, or any destructive operations. Copilot provides its best suggestion, but you are responsible for verifying it is safe and correct for your specific environment.Troubleshooting Common Issues
If you run into problems with Copilot in the CLI, here are the most common issues and their solutions:
- "Not authenticated" - Run
gh auth loginand ensure you are logged in with an account that has Copilot access - "Extension not found" - Reinstall with
gh extension install github/gh-copilot. If updating, usegh extension upgrade gh-copilot - Slow responses - Copilot CLI requires an internet connection. Check your connectivity and note that complex queries may take a few seconds
- Incorrect OS-specific commands - Explicitly state your operating system in the query (e.g., "on Windows using PowerShell")
- Outdated suggestions - Keep the extension updated with
gh extension upgrade gh-copilotto get the latest model improvements
explain command is your best tool for building that understanding over time.Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX