简介
通过 GitHub Copilot 的命令行接口 (CLI),你可以直接从终端使用 Copilot。 你可以使用该接口来回答问题、编写和调试代码,以及与 GitHub.com 进行交互。 例如,你可以让 Copilot 对项目进行一些更改并创建拉取请求。
GitHub Copilot 命令行界面 使你能够快速使用强大的 AI 智能体,而无需离开终端。 它可以代表你执行操作,帮助你更快地完成任务,而且你可以与 GitHub Copilot 命令行界面 进行迭代协作,构建所需的代码。
注意
GitHub Copilot 命令行界面 正在 使用数据保护的公共预览版 阶段,并可能会更改。
受支持的操作系统
- Linux
- macOS
- 适用于 Linux 的 Windows 子系统 (WSL) 中的 Windows。 PowerShell 中已提供本机 Windows 支持,但该支持目前处于试验阶段。
有关安装说明,请参阅“安装 GitHub Copilot CLI”。
使用模式
GitHub Copilot 命令行界面 可在两种模式下使用:
-
**交互模式**:通过使用 `copilot` 命令启动交互式会话。 这是使用 CLI 时的默认模式。在此模式下,你可以提示 Copilot 回答问题或执行任务。 在同一会话中,你可以对 Copilot 的响应做出反应。

-
**编程模式**:你也可以直接在命令行上向 CLI 传递单个提示。 可通过使用 `-p` 或 `--prompt` 命令行选项来实现这一点。 要允许 Copilot 修改和执行文件,还应使用其中一个批准选项(详见本文后文的[允许工具无需手动批准即可使用](#allowing-tools-to-be-used-without-manual-approval))。 例如:Bash copilot -p "Show me this week's commits and summarize them" --allow-tool 'shell(git)'
copilot -p "Show me this week's commits and summarize them" --allow-tool 'shell(git)'或者,可以使用脚本输出命令行选项,并通过管道将其传递给
copilot。 例如:Bash echo ./script-outputting-options.sh | copilot
echo ./script-outputting-options.sh | copilot
注意
如果使用诸如 --allow-all-tools 之类的自动批准选项,Copilot 将拥有与你相同的计算机文件访问权限,并且可以运行你能运行的任何 shell 命令,而无需事先获得你的批准。 请参阅本文后面的安全注意事项。
GitHub Copilot 命令行界面 的用例
以下各节提供了可通过 GitHub Copilot 命令行界面 完成的任务示例。
本地任务
-
在项目目录中,你可以让 Copilot 对项目中的代码进行更改。 例如:
Change the background-color of H1 headings to dark blueCopilot 会找到定义 H1 标题样式的 CSS 文件,并更改其颜色值。
-
让 Copilot 告知你某个文件的更改情况:
Show me the last 5 changes made to the CHANGELOG.md file. Who changed the file, when, and give a brief summary of the changes they made -
使用 Copilot 帮助改进项目中的代码或文档。
-
Suggest improvements to content.js -
Rewrite the readme in this project to make it more accessible to newcomers
-
-
使用 Copilot 帮助执行 Git 操作。
-
Commit the changes to this repo -
Revert the last commit, leaving the changes unstaged
-
-
让 Copilot 从头创建一个应用程序,例如作为概念证明。
Use the create-next-app kit and tailwind CSS to create a next.js app. The app should be a dashboard built with data from the GitHub API. It should track this project's build success rate, average build duration, number of failed builds, and automated test pass rate. After creating the app, give me easy to follow instructions on how to build, run, and view the app in my browser. -
让 Copilot 解释其做出的某项更改为何未按预期运行,或者让 Copilot 修复上一次更改所导致的问题。 例如:
You said: "The application is now running on http://localhost:3002 and is fully functional!" but when I browse to that URL I get "This site can't be reached"
涉及 GitHub.com
的任务
-
从 GitHub.com 提取并显示你的工作详细信息。
-
List my open PRs这列出了你从 GitHub 上的任何存储库发出的打开拉取请求。 有关更具体的结果,请在提示中包含仓库名称:
-
List all open issues assigned to me in OWNER/REPO
-
-
让 Copilot 处理某个议题:
I've been assigned this issue: https://github.com/octo-org/octo-repo/issues/1234. Start working on this for me in a suitably named branch. -
让 Copilot 进行文件更改并在 GitHub.com 上创建拉取请求。
-
In the root of this repo, add a Node script called user-info.js that outputs information about the user who ran the script. Create a pull request to add this file to the repo on GitHub. -
Create a PR that updates the README at https://github.com/octo-org/octo-repo, changing the subheading "How to run" to "Example usage"
Copilot 代表你在 GitHub.com 上创建拉取请求。 你会被标记为该拉取请求的创建者。
-
-
让 Copilot 在 GitHub.com 上为你创建一个议题。
Raise an improvement issue in octo-org/octo-repo. In src/someapp/somefile.py the `file = open('data.txt', 'r')` block opens a file but never closes it. -
让 Copilot 检查拉取请求中的代码更改。
Check the changes made in PR https://github.com/octo-org/octo-repo/pull/57575. Report any serious errors you find in these changes.Copilot 会在 CLI 中返回它发现的所有问题的摘要。
-
管理来自 GitHub Copilot 命令行界面 的拉取请求。
-
Merge all of the open PRs that I've created in octo-org/octo-repo -
Close PR #11 on octo-org/octo-repo
-
-
查找特定议题类型。
Use the GitHub MCP server to find good first issues for a new team member to work on from octo-org/octo-repo注意
如果你知道某个特定的 MCP 服务器能够完成某项特定任务,那么在提示中明确指定该服务器,有助于 Copilot 给出你想要的结果。
-
查找特定的 GitHub Actions 工作流。
List any Actions workflows in this repo that add comments to PRs -
创建 GitHub Actions 工作流。
Branch off from main and create a GitHub Actions workflow that will run on pull requests, or can be run manually. The workflow should run eslint to check for problems in the changes made in the PR. If warnings or errors are found these should be shown as messages in the diff view of the PR. I want to prevent code with errors from being merged into main so, if any errors are found, the workflow should cause the PR check to fail. Push the new branch and create a pull request.
安全注意事项
使用 Copilot 命令行界面(CLI) 时,Copilot 可以代表你执行各种任务,例如执行或修改文件、运行 shell 命令等。
因此,使用 Copilot 命令行界面(CLI) 时,你应始终将安全注意事项放在心上,正如你亲自处理文件或直接在终端中运行命令时一样。 当 Copilot 命令行界面(CLI) 请求你批准时,应始终仔细检查建议的命令。
受信任目录
启动 GitHub Copilot 命令行界面 会话时,系统会要求你确认是否信任从其启动 CLI 的目录及其子目录中的文件。
警告
- 应只从你信任的目录中启动 Copilot 命令行界面(CLI)。 不应在可能包含你无法确定是否可信的可执行文件的目录中使用 Copilot 命令行界面(CLI)。 同样,如果从包含敏感或机密数据,或者你不希望被修改的文件的目录中启动 CLI,可能会无意中使这些文件面临风险。 通常情况下,不应从主目录中启动 Copilot 命令行界面(CLI)。
- 权限的范围界定是基于启发式原则的,并且 GitHub 不保证所有位于受信任目录之外的文件都能得到保护。 请参阅本文后面部分的风险缓解。
你可以选择信任当前目录以实现以下目的:
- 仅当前正在运行的会话
- 此会话和未来会话
如果选择信任目录以供将来的会话使用,则将不再显示受信任的目录提示。 只有当你确定该位置始终是 Copilot 安全运行的环境时,才应选择第二个选项。
可通过修改 CLI 的 config.json 文件中的 trusted_folders 数组的内容,来编辑永久信任的目录列表。 默认情况下,该文件位于 ~/.copilot 目录中。 你可以通过设置 XDG_CONFIG_HOME 环境变量来更改此位置。
允许的工具
当 Copilot 首次需要使用可能用于修改或执行文件的工具时(例如 例如 touch、chmod、node 或 sed 中所列的工具),它会询问你是否允许使用该工具。
通常,可从以下三个部署选项中进行选择:
1. Yes
2. Yes, and approve TOOL for the rest of the running session
3. No, and tell Copilot what to do differently (Esc)
**选项 1**,允许 Copilot 仅在本次运行此特定命令。 下次需要使用此工具时,它会再次询问你。
**选项 2**,允许 Copilot 在当前运行的会话期间再次使用该工具,无需再次请求你的许可。 在新的会话中,或者如果你在未来重新恢复当前会话时,它会再次请求你的批准。 如果选择此选项,就意味着允许 Copilot 以它认为合适的任何方式使用该工具。 例如,如果 Copilot 请求你允许它运行命令 `rm ./this-file.txt`,而你选择了选项 2,那么在当前会话运行期间,Copilot 可以运行任何 `rm` 命令(例如 `rm -rf ./*`),而无需再次请求你的批准。
**选项 3**,可取消提议的命令,并允许你告知 Copilot 尝试其他方法。
允许在未经手动批准的情况下使用工具
有三个命令行选项可用于交互模式或编程模式,用于确定 Copilot 无需请求你批准即可使用的工具:
-
--allow-all-tools允许 Copilot 无需请求你批准即可使用任何工具。
例如,你可以在编程模式中使用此选项,让 CLI 能够运行任何命令。 例如:
copilot -p "Revert the last commit" --allow-all-tools -
--deny-tool阻止Copilot 使用特定工具。
此选项优先于
--allow-all-tools和--allow-tool选项。 -
--allow-tool允许 Copilot 无需请求你批准即可使用特定工具。
使用批准选项
--deny-tool 和 --allow-tool 选项需要以下参数之一:
-
'shell(COMMAND)'例如,
copilot --deny-tool 'shell(rm)'可以阻止 Copilot 使用任何rm命令。对于
git和gh命令,可以指定要允许或拒绝的特定一级子命令。 例如:copilot --deny-tool 'shell(git push)'工具规范是可选的。 例如,
copilot --allow-tool 'shell'允许 Copilot 使用任何 shell 命令而无需单独批准。 -
'write'此参数用于允许或禁止除 shell 命令外的工具获得修改文件的权限。
例如,
copilot --allow-tool 'write'允许 Copilot 编辑文件而无需你的单独批准。 -
'MCP_SERVER_NAME'此参数用于允许或禁止指定 MCP 服务器中的工具,其中
MCP_SERVER_NAME是你已配置的某个 MCP 服务器的名称。 服务器中的工具在括号中指定,并使用向 MCP 服务器注册的工具名称。 仅使用服务器名称而未指定具体工具,将允许或拒绝该服务器中的所有工具。例如,
copilot --deny-tool 'My-MCP-Server(tool_name)'可阻止 Copilot 使用来自名为My-MCP-Server的 MCP 服务器中名为tool_name的工具。你可以通过在 Copilot 命令行界面(CLI) 的交互模式中输入
/mcp,然后从显示的列表中选择服务器,来查找 MCP 服务器的名称。
合并审批选项
你可以使用多种批准选项组合,精确确定 Copilot 无需请求你批准即可使用的工具范围。
例如,要阻止 Copilot 使用 rm 和 git push 命令,但自动允许所有其他工具,可使用:
copilot --allow-all-tools --deny-tool 'shell(rm)' --deny-tool 'shell(git push)'
要阻止 Copilot 使用名为 My-MCP-Server 的 MCP 服务器中的 tool_name 工具,但允许该服务器上的所有其他工具无需单独批准即可使用,可使用:
copilot --allow-tool 'My-MCP-Server' --deny-tool 'My-MCP-Server(tool_name)'
自动工具批准的安全问题
必须认识到使用批准类命令行选项所涉及的安全问题。 这些选项允许 Copilot 执行完成请求所需的命令,而不会让你有机会在这些命令运行前查看和批准它们。 虽然这可简化工作流程并实现 CLI 的无头操作,但也会增加执行意外操作的风险,可能导致数据丢失、数据损坏或其他安全问题。
风险缓解
你可通过在受限环境中使用 Copilot 命令行界面(CLI) 来降低自动批准选项的相关风险,例如使用无 Internet 访问的虚拟机、容器或专用系统。 这可限制在允许 Copilot 执行你尚未审阅和验证的命令时,可能发生的任何潜在损害。
模型使用
GitHub Copilot 命令行界面 使用的默认模型为 克劳德十四行诗 4.5。 GitHub 保留更改此模型的权利。
可以使用 /model 斜杠命令更改 GitHub Copilot 命令行界面 中使用的模型。 输入此命令,然后从列表中选择一个模型。
每次在 Copilot 命令行界面(CLI) 的交互模式下向 Copilot 提交提示时,以及每次以编程模式使用 Copilot 命令行界面(CLI) 时,你的 Copilot 高级请求的月度配额都会减少一个,每次减少量将乘以模型列表中括号内显示的乘数。 例如,Claude Sonnet 4.5 (1x) 表示使用此模型,每次提交提示时,你的高级请求配额将减少一个。 有关高级请求的信息,请参阅 GitHub Copilot 中的请求。
反馈
如果你对 GitHub Copilot 命令行界面 有任何反馈,请通过交互式会话中的 /feedback 斜杠命令选择相应选项告知我们。 你可以选择参与私密反馈调查、提交 bug 报告或提出新功能建议。
其他阅读材料
-
[AUTOTITLE](/copilot/how-tos/set-up/install-copilot-cli) -
[AUTOTITLE](/copilot/how-tos/use-copilot-agents/use-copilot-cli) -
[AUTOTITLE](/enterprise-cloud@latest/copilot/responsible-use/copilot-cli)