Skip to main content

Using GitHub Copilot in the CLI

You can use gh, the GitHub command line interface, to work with GitHub Copilot in the CLI.

About using GitHub Copilot in the CLI

GitHub CLI is an open source tool for using GitHub from your computer's command line. When you're working from the command line, you can use the GitHub CLI to save time and avoid switching context. For more information, see "About GitHub CLI."

GitHub Copilot in the CLI is an extension for GitHub CLI which provides a chat-like interface in the terminal that allows you to ask questions about commands you run from the command line. You can ask Copilot in the CLI to suggest a command for your use case, with gh copilot suggest, or to explain a command you're curious about, with gh copilot explain.

Additionally, you can use gh copilot --help for general help or gh copilot SUBCOMMAND --help for help with a specific subcommand.

Prerequisites

To access GitHub Copilot in the CLI you will need the following.

  • Subscription to GitHub Copilot: You must have an active GitHub Copilot subscription. For more information, see "About billing for GitHub Copilot."
  • Access to Copilot in the CLI: If you are part of an organization or enterprise with a GitHub Copilot Business or GitHub Copilot Enterprise subscription, the organization or enterprise owner will need to grant you access to Copilot in the CLI. For more information, see "Setting up GitHub Copilot in the CLI."
  • Install GitHub CLI: You must have GitHub CLI installed. For installation instructions for GitHub CLI, see the GitHub CLI repository.
  • Install the Copilot in the CLI extension: Additionally, you must have the Copilot in the CLI extension installed. For more information, see "Installing Copilot in the CLI."

Asking Copilot in the CLI to explain a command

You can ask Copilot in the CLI to explain a command for you by running:

Shell
gh copilot explain

Alternatively, you can add the command you want explained directly to the prompt:

Shell
gh copilot explain "sudo apt-get"

Copilot in the CLI can help by explaining what a command does in plain language. This makes it easier for you to understand the command's purpose and how it works for a specific example. You don't need to go through the command's documentation because the explanation includes information about what the command takes as input and produces as output, and provides practical examples.

Asking Copilot in the CLI to suggest a command

You can ask Copilot in the CLI to suggest a command for you by running:

Shell
gh copilot suggest

This will start an interactive experience to get the command you need. Copilot in the CLI aims to suggest commands that help you perform the tasks you’re trying to complete. To help Copilot in the CLI provide better suggestions, you can specify the type of command you are looking for (generic, git or gh).

If you already know what command you need, you can also include that in the prompt. For example, if you want to install Git, you can ask Copilot in the CLI to suggest a command for you:

Shell
gh copilot suggest "Install git"

If the result isn’t quite what you’re looking for, you can keep revising your question until the returned command meets your expectations. You can do this by selecting the Revise command option.

Once you’ve generated the perfect command for your task, you can easily copy it to your clipboard to run it wherever you need by selecting the Copy to clipboard option, or you can execute the command.

Executing the command

After Copilot in the CLI suggests a command, you can choose to execute the command by selecting the Execute command option. If you select this option after running gh copilot suggest to ask for a suggestion, Copilot in the CLI will copy the command to your clipboard and exit out of the interactive session automatically. You can then paste the command and run it yourself.

If you want Copilot in the CLI to execute commands for you, the ghcs alias generated by gh copilot alias provides this support. For more information about configuring the ghcs alias, see "Setting up aliases for Copilot in the CLI."

When you use the ghcs alias and you select Execute command, Copilot in the CLI will ask for your confirmation before executing the command. The confirmation prompt defaults to No, but you can change your preference via the gh copilot config option. For more information, see "Configuring GitHub Copilot in the CLI."

If you confirm, the command will be executed and saved to your shell history for future reference.

Setting up aliases for Copilot in the CLI

With gh copilot alias, you can create aliases for Copilot in the CLI. Aliases are not required to use Copilot in the CLI, but they can be useful for convenience as they require fewer keystrokes and aid in executing suggested commands.

You can also use the provided aliases to wrap gh copilot suggest and gh copilot explain. The provided aliases are ghcs and ghce. To use ghcs and ghce, you'll need to add some alias configurations to your shell configuration file.

The following examples show how to add the alias configurations to your Bash, PowerShell, and Zsh configuration files.

Bash

Run the following to add the aliases to your Bash configuration file:

Shell
echo 'eval "$(gh copilot alias -- bash)"' >> ~/.bashrc

PowerShell

Run the following to add the aliases to your PowerShell profile:

Shell
$GH_COPILOT_PROFILE = Join-Path -Path $(Split-Path -Path $PROFILE -Parent) -ChildPath "gh-copilot.ps1"
gh copilot alias -- pwsh | Out-File ( New-Item -Path $GH_COPILOT_PROFILE -Force )
echo ". `"$GH_COPILOT_PROFILE`"" >> $PROFILE

Zsh

Run the following to add the aliases to your Zsh configuration file:

Shell
echo 'eval "$(gh copilot alias -- zsh)"' >> ~/.zshrc

Once you have the alias set up, you can run the following:

Shell
ghcs 'print "Hello world!"'

Sharing feedback about Copilot in the CLI

If you encounter any issues or limitations with Copilot in the CLI, you can provide feedback by selecting the Rate response option in Copilot in the CLI.