Skip to main content

使用 GitHub Copilot(CLI 版)

可以将 GitHub 命令行界面 gh 与 GitHub Copilot in the CLI 一起使用。

关于使用 GitHub Copilot in the CLI

GitHub CLI 是用于从计算机的命令行使用 GitHub 的开源工具。 从命令行操作时,您可以使用 GitHub CLI 来节省时间并避免切换上下文。有关详细信息,请参阅“关于 GitHub CLI”。

GitHub Copilot in the CLI 是 GitHub CLI 的扩展,其在终端中提供类似聊天的界面,允许提出有关在命令行运行命令的问题。你可以使用 gh copilot suggest 请求 Copilot in the CLI 为你的用例建议命令,也可以使用 gh copilot explain 请求解释你感兴趣的命令。

或者,可以使用 gh copilot --help 获取常规帮助,使用 gh copilot SUBCOMMAND --help 获取特定子命令的帮助。

先决条件

若要访问 GitHub Copilot in the CLI,需要具备以下条件。

  • GitHub Copilot 订阅:必须拥有有效的 GitHub Copilot 订阅。 有关详细信息,请参阅“关于 GitHub Copilot 的计费”。
  • Copilot in the CLI 的访问权限:如果你是拥有 GitHub Copilot Business 订阅的组织的一部分,则组织所有者将需要授予你对 Copilot in the CLI 的访问权限。 有关详细信息,请参阅“在 CLI 中设置 GitHub Copilot”。
  • 安装 GitHub CLI:必须先安装 GitHub CLI。 有关 GitHub CLI 的安装说明,请参阅 GitHub CLI 存储库
  • 安装 Copilot in the CLI 扩展:此外,必须已安装 Copilot in the CLI 扩展。 有关详细信息,请参阅安装 Copilot in the CLI

要求 Copilot in the CLI 解释命令

可以通过运行以下命令要求 Copilot in the CLI 为你解释命令:

Shell
gh copilot explain

或者,也可以将需要说明的命令直接添加到提示:

Shell
gh copilot explain "sudo apt-get"

Copilot in the CLI 可以通过用简单语言解释命令的功能来提供帮助。 这使你能够更轻松地了解命令的用途,及其在特定示例中的作用。 不需要查看命令的文档,因为解释中包含了命令所接收的输入和生成的输出的信息,并提供了实际示例。

要求 Copilot in the CLI 提供建议命令

可以通过运行以下命令要求 Copilot in the CLI 为你建议命令:

Shell
gh copilot suggest

这将启动交互式体验来获取所需的命令。 Copilot in the CLI 旨在建议有助于执行要完成的任务的命令。 为了帮助 Copilot in the CLI 提供更好的建议,可以指定要查找的命令类型(通用,gitgh)。

如果已经知道所需的命令,还可以在提示中包含该命令。 例如,如果要安装 Git,可以通过运行以下命令要求 Copilot in the CLI 为你建议命令:

Shell
gh copilot suggest "Install git"

如果结果不完全符合你的要求,你可以不断修改问题,直到返回的命令满足期望。 可以通过选择“修改命令”**** 选项来执行此操作。

生成任务的完美命令后,可以通过选择“复制到剪贴板”**** 选项轻松将其复制到剪贴板以根据需要运行它,或者你可以执行命令。

执行命令

当 Copilot in the CLI 建议命令后,你可以选择通过选择“执行命令”**** 选项来执行命令。 如果你在运行 gh copilot suggest 后选择此选项以请求建议,Copilot in the CLI 会将命令复制到剪贴板并自动退出交互式会话。 然后,你可以粘贴该命令并自行运行它。

如果你希望 Copilot in the CLI 为你执行命令,则由 gh copilot alias 生成的 ghcs 别名将提供此支持。 有关配置 ghcs 别名的详细信息,请参阅“为 Copilot in the CLI 设置别名”。

当你使用 ghcs 别名并选择“执行命令”**** 时,则 Copilot in the CLI 会在执行该命令之前要求你确认。 确认提示默认为 No,但你可以通过 gh copilot config 选项来更改首选项。 有关详细信息,请参阅“在 CLI 中配置 GitHub Copilot”。

如果确认,该命令将被执行并保存到 shell 历史记录中以供将来参考。

设置 Copilot in the CLI 的别名

使用 gh copilot alias,你可以为 Copilot in the CLI 创建别名。 使用 Copilot in the CLI 不需要别名,它们使用方便,因为它们需要更少的击键,并有助于执行建议的命令。

你还可以使用提供的别名来整合 gh copilot suggestgh copilot explain。 提供的别名为 ghcsghce。 要使用 ghcsghce,你需要向 shell 配置文件添加一些别名配置。

以下示例演示如何将别名配置添加到你的 Bash、PowerShell 和 Zsh 配置文件。

Bash

运行以下内容以将失真添加到你的 Bash 配置文件:

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

PowerShell

运行以下内容以将失真添加到 PowerShell 配置文件:

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

运行以下内容以将失真添加到你的 Zsh 配置文件:

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

设置别名后,你即可运行以下命令:

Shell
ghcs 'print "Hello world!"'

共享有关 Copilot in the CLI 的反馈

如果 Copilot in the CLI 遇到任何问题或限制,可以通过选择 Copilot in the CLI 中的“速率响应”**** 选项来提供反馈。