Copilot CLI

GitHub Copilot CLI 把可代理的编码助手放进终端:交互会话、一次性 -p 提示、以及和 Issue / PR 相关的工作流。官方:

需要有效的 Copilot 访问权限。组织若禁用 CLI,登录成功也跑不了。命令、斜杠指令与安装包名称以你安装当天的 copilot help / /help 为准。


系统要求

平台注意
Windows官方要求 PowerShell 6+(推荐 PowerShell 7)。WinGet 包名 GitHub.Copilot
macOS / LinuxHomebrew cask 或官方安装脚本
全平台 npmNode.js 22+,包名 @github/copilot

在 PowerShell 7 里看版本:

$PSVersionTable.PSVersion
node -v

macOS:

node -v

安装

npm(全平台,需 Node 22+):

npm install -g @github/copilot

~/.npmrc 里有 ignore-scripts=true,官方要求改用:

npm_config_ignore_scripts=false npm install -g @github/copilot

Windows(WinGet):

winget install GitHub.Copilot

macOS / Linux(Homebrew):

brew install --cask copilot-cli

macOS / Linux(脚本):

curl -fsSL https://gh.io/copilot-install | bash

需要装到 /usr/local/bin 时可 | sudo bash。自定义前缀用环境变量 PREFIX;钉版本用 VERSION。也可从 github/copilot-cli releases 下可执行文件。

验证:

copilot --version

Windows 若找不到命令:关掉终端重开,确认 WinGet 的链接目录在 PATH 里。


第一次交互会话

项目目录里启动(这样它才能看到仓库文件):

cd path/to/your-repo
copilot
  1. 若未登录,输入 /login,按设备码 / 浏览器提示用 GitHub 账号授权。
  2. 确认你信任当前目录适合交给 AI。官方注明:没有你的明确批准,它不会改文件。
  3. 试一句:Give me an overview of this project.

也可用带 Copilot Requests 权限的细粒度 PAT,按优先级导出 COPILOT_GITHUB_TOKENGH_TOKENGITHUB_TOKEN。不要把 token 提交进 Git。


常用快捷键与斜杠

Getting started 中的核心快捷键:

按键作用
Esc取消当前操作
Ctrl+C思考中则取消;否则清空输入或退出
Ctrl+L清屏
@提及文件纳入上下文
/斜杠命令
?分页帮助
/ 命令历史

完整列表:会话里 /help,或终端 copilot help。文档还提到 /plan/pr/fleet/research 等——以你的 CLI 版本帮助为准,不要背过期命令。


非交互:-p 与脚本

不进入 TUI,适合脚本:

copilot -p "In Git, how can I apply a commit from another branch"

只要模型回复、不要额外用量装饰时加 -s

copilot -sp "List the test command quoted from this repo's package.json. Do not invent scripts."

更多 flag:copilot help。把 CLI 嵌进 GitHub Actions 时,走官方 Running GitHub Copilot CLI programmaticallyUsing Copilot CLI in GitHub Actions with GITHUB_TOKEN,不要把个人 PAT 写进工作流日志。


和 IDE / Cursor 怎么分工

  • CLI:你已经在 SSH、CI、纯键盘环境;或要 /pr 这类终端里的 GitHub 工作流
  • VS Code Chat:盯着 diff 点选文件
  • Cursor:AI 原生编辑器,见 对比

可以在本机同时装 Copilot CLI 与 Cursor 的 agent,但不要让两个 Agent 同时写同一工作区 却不提交——冲突难解。Windows 上把默认终端设成 PowerShell 7,避免 CLI 掉进 Windows PowerShell 5.1。


下一步

评论