GitHub Copilot CLI 使用实践要点
介绍 GitHub Copilot CLI 在终端中作为编码助手的若干实践,包括用自定义指令文件统一项目约定、在写代码前先进入计划模式生成结构化实现方案,以及通过 /delegate 把可异步执行的任务交给云端代理处理。
## [导言](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#introduction>) GitHub 副驾驶CLI是一个终端本地的AI编码助理,可以将代理能力直接带入您的指令线. 副驾驶CLI可以像聊天员一样操作,回答您的问题,但其真正的力量在于其作为您的编码伙伴自主工作的能力,允许您委托任务并监督其工作. 文章提供了从 Copilot CLI 中获取最多帮助的提示, 从有效使用各种 CLI 命令到管理 CLI 访问文件的提示。 将这些提示视为起点, 然后进行实验, 找出对您的工作流程最有效的选择 。 说明 GitHub 副驾驶 CLI 正在不断发展。 `/help` 命令以查看最新的信息。 ## [1. 自定义您的环境](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#1-customize-your-environment>) ### [使用自定义指令文件](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#use-custom-instructions-files>) 副驾驶 CLI 自动结合了应用的用户级,寄存器,和路径特定指令. 将寄存器指令用于项目常规,并使用用户级指令,用于跨项目应用的首选项. 关于支持位置的完整列表和关于发现,文件引用,以及多个指令文件如何互动的详细信息,请参见 [为 GitHub 添加自定义指令 副驾驶 CLI](<https://docs.github.com/en/copilot/how-tos/copilot-cli/customize-copilot/add-custom-instructions>). #### [最佳做法](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#best-practice>) 避免指令冲突。例如,这是一个简单的 `.github/copilot-instructions.md` 文档。 ```markdown ## Build Commands - `npm run build` - Build the project - `npm run test` - Run all tests - `npm run lint:fix` - Fix linting issues ## Code Style - Use TypeScript strict mode - Prefer functional components over class components - Always add JSDoc comments for public APIs ## Workflow - Run `npm run lint:fix && npm test` after making changes - Commit messages follow conventional commits format - Create feature branches from `main` ``` 提示( T) 保持指令的简洁和可操作性,长度指令可以淡化有效性. 更多信息见: [GitHub 辅助驾驶的自定义指令](<https://docs.github.com/en/copilot/concepts/prompting-and-context/custom-instructions>). ### [配置允许的工具](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#configure-allowed-tools>) 管理哪些工具可无需请求您的许可即可使用。 在手动批准模式中, 尚未允许的请求可以提示您批准。 这些请求可以包括运行命令、 编辑文件或访问 URL 。 当被提醒时, 您通常可以选择仅允许此一次操作, 或者允许该工具用于会话的其余部分 。 使用 。 `/permissions show` 来检查您的模式或 `/permissions manual` 选择人工审批。 要重置先前核准的工具,请使用: ```copilot /reset-allowed-tools ``` 您也可以通过 CLI 旗帜预配置允许的工具 : ```bash copilot --allow-tool='shell(git:*)' --deny-tool='shell(git push)' ``` **常见权限模式 :** - `shell(git:*)` —— 允许所有 Git 命令 - `shell(npm run:*)` ——允许所有 npm 脚本 - `shell(npm run test:*)` — 允许 npm 测试命令 - `write` ——允许文件写入 关于允许和拒绝允许特定工具的更多信息,见: [允许和拒绝使用工具](<https://docs.github.com/en/copilot/how-tos/copilot-cli/use-copilot-cli/allowing-tools#allowing-or-denying-permission-for-specific-tools>). ### [选择您的首选模式](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#select-your-preferred-model>) 使用( U) `/model` 根据任务复杂度从现有模型中选择: | 型号 | 最佳时间 | 权衡 | | --- | --- | --- | | **副驾驶融合** | 提供多种努力水平,使你能够确定系统为每个时速确定优先的质量、成本和能力权衡的优先顺序。 | 见 [关于GitHub 副驾驶融合](<https://docs.github.com/en/copilot/concepts/models/copilot-fusion>) | | **克劳德·奥普斯·5.5** | 结构复杂,调试困难,重构细微 | 能力最强,但成本更高 | | **克劳德·索纳特 5** | 日常编码, 大多数常规任务 | 快速,成本效益高,处理得最好 | | **GPT-5.3-Codex** | 代码生成、代码审查、直接执行 | 对审查其他模型生成的代码非常出色 | **建议:** - **副驾驶融合** 是一个高级运行时的模型路由器和管弦乐器。它为您的任务选择模型和执行模式,然后返回单个响应。 - **克劳德·奥普斯·5.5** 对于需要深刻推理,复杂的系统设计,微妙的bug调查,或广泛的上下文理解的任务来说,是理想的. - **切换到 Claude Sonnet 5 中** 在速度和成本效率都很重要的日常任务中,它有效地处理大部分日常编码。 - **使用 GPT-5.3-Codex 软件** 作为审查其他模型生成的代码的第二个意见。 您可以在会话中切换模型 `/model` 作为任务复杂度的变化。 如果您的组织或企业已使用自己的 LLM 提供者 API 密钥配置了自定义模式,这些模式也会出现在 `/model` 在列表的底部。 ### [使用自己的模型提供者](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#use-your-own-model-provider>) 您可以配置副驾驶 CLI , 使用自己的模型提供者, 而不是 GitHub 托管的模型。 运行 `copilot help providers` 完全设置指令。 **主要考虑因素:** - 你的模特儿必须支持 **工具调用** (公务电话)和 **流线**。如果缺少任何一种能力,副驾驶CLI返回一个错误。 - 对于最佳效果,使用至少是128k令牌的上下文窗口的模型. - 内建副剂( )`/review`, `/task`探索,探索, `/fleet`)自动继承您的提供者配置。 - 使用您自己的提供者时, 成本估计会隐藏。 托肯用法( 输入、 输出和缓存计数) 仍然显示 。 - `/delegate` 仅当您也被签名到 GitHub 时才有效 。 它将会话传输到 GitHub 的服务器侧副驾驶, 而不是您的提供者 。 见 [使用自己的模型提供者](<https://docs.github.com/en/copilot/concepts/copilot-surfaces/copilot-cli#using-your-own-model-provider>). ### [设定 AI 信用会话限制](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#set-ai-credit-session-limits>) 您可以为AI 信用额度设定上限, 由 Copilot 用于单个会话, 以便长期或复杂的任务不会消耗比预期更多的资源 。 见 。 [在 GitHub 中设置 AI 信用会话限制 副驾驶 CLI](<https://docs.github.com/en/copilot/how-tos/copilot-cli/use-copilot-cli/set-session-limit>). 更多关于如何优化您的 AI 信用使用的信息,请参见 [优化您的 AI 使用,以最大限度地提高效率和降低成本](<https://docs.github.com/en/copilot/tutorials/optimize-ai-usage>). ## [2. 密码前的计划](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#2-plan-before-you-code>) ### [计划模式](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#plan-mode>) **模式在得到具体计划后,成功率会更高。** 在计划模式下,副驾驶将在任何代码被写入之前创建一个结构化的实施计划. 按 Shift+Tab 键在正常模式和计划模式之间切换。在计划模式中,所有输入的提示都会触发计划工作流程。 或者,你可以使用 `/plan` 命令在正常模式下实现相同效果。 **示例提示( 从正常模式) :** ```copilot /plan Add OAuth2 authentication with Google and GitHub providers ``` **发生什么了?** - 副驾驶分析您的请求和密码库. - **回答问题** 使各项要求和方法保持一致。 - 创建一个带有复选框的结构化实施计划。 - 将计划保存到 `plan.md` 在您的会话文件夹中。 - **等待您的批准 等待您的批准 等待您的批准** 执行之前。 您可以按 Ctrl+y 查看和编辑 Markdown 文件默认编辑器中的计划 。 **示例计划产出 :** ```markdown # Implementation Plan: OAuth2 Authentication ## Overview Add social authentication using OAuth2 with Google and GitHub providers. ## Tasks - [ ] Install dependencies (passport, passport-google-oauth20, passport-github2) - [ ] Create authentication routes in `/api/auth` - [ ] Implement passport strategies for each provider - [ ] Add session management middleware - [ ] Create login/logout UI components - [ ] Add environment variables for OAuth credentials - [ ] Write integration tests ## Detailed Steps 1. **Dependencies**: Add to package.json... 2. **Routes**: Create `/api/auth/google` and `/api/auth/github`... ``` ### [何时使用计划模式](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#when-to-use-plan-mode>) | 假设 | 使用计划模式吗 ? | | --- | --- | | 复杂的多文件更改 | | | 以许多触点重构 | | | 新特性执行 | | | 快速错误修正 | | | 单文件更改 | | ### [探索 计划 代码 承诺工作流程](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#the-explore--plan--code--commit-workflow>) 复杂任务的最佳成果: - **探索**: `Read the authentication files but don't write code yet` - **计划**: `/plan Implement password reset flow` - **审查**: 检查计划,建议修改 - **执行**: `Proceed with the plan` - **校验**: `Run the tests and fix any failures` - **提交**: `Commit these changes with a descriptive message` ## [3. 利用无限会议](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#3-leverage-infinite-sessions>) ### [自动上下文窗口管理](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#automatic-context-window-management>) 副驾驶 CLI 特性 **无限届会**。您不需要担心断章取义。系统通过智能压缩自动管理上下文,在保存重要信息的同时总结对话历史。 **会话存储位置 :** ```text ~/.copilot/session-state/{session-id}/ ├── events.jsonl # Full session history ├── workspace.yaml # Metadata ├── plan.md # Implementation plan (if created) ├── checkpoints/ # Compaction history └── files/ # Persistent artifacts ``` 说明 如果你需要手动触发紧凑,请使用 `/compact`。由于系统自动处理,因此这很少有必要。 ### [会话管理命令](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#session-management-commands>) 要查看当前CLI会话的信息,请输入: ```copilot /session info ``` 要查看任何会议检查站的清单,请输入: ```copilot /session checkpoints ``` 说明 当会话上下文被压缩时会创建一个检查点,并允许您查看 Copilot 创建的概要上下文 。 要查看特定检查站的细节,请输入: ```copilot /session checkpoints NUMBER ``` 地点NUMBER指定要显示的检查点。 要查看当前会话中创建的任何临时文件, 例如, 由 Copilot 创建的不该保存到寄存器的文物 —— 进入 : ```copilot /session files ``` 要查看当前计划( 如果副驾驶生成了一个), 请输入 : ```copilot /session plan ``` ### [最佳做法:使会议有重点](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#best-practice-keep-sessions-focused>) 虽然无限会议允许长期工作,但重点会议能产生更好的结果: - 使用( U) `/clear` 或 时 间 `/new` 在不相关的任务之间。 - 此重排上下文, 并提高响应质量 。 - 想想这就像开始 与同事的新对话。 ### [那个... `/context` 命令](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#the-context-command>) 可视化您当前上下文的使用 `/context`显示下列细目: - 系统/工具符号 - 信件历史符号 - 空闲空间 - 缓冲分配 ## [4. 有效代表工作](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#4-delegate-work-effectively>) ### [那个... `/delegate` 命令](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#the-delegate-command>) **卸载工作在云中运行 使用副驾驶云代理。** 这对于: - 可以同步运行的任务 。 - 更改其他寄存器 。 - 长期运行的操作你不想等待. **示例提示 :** ```copilot /delegate Add dark mode support to the settings page ``` **发生什么了?** - 您的请求被发送到副驾驶云层代理。 - 代理机随更改而创建拉动请求. - 在云代理工作期间,您可以继续在当地工作. ### [何时使用 `/delegate`](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#when-to-use-delegate>) | 使用( U) `/delegate` | 当地工作 | | --- | --- | | 切换任务 | 核心特征工作 | | 文件最新情况 | 调试 | | 重构单独的模块 | 互动探索 | ## [5. 共同工作流程](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#5-common-workflows>) ### [登机码库](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#codebase-onboarding>) 在加入新项目时, 使用副驾驶 CLI 作为您的配对编程伙伴。 例如, 您可以询问副驾驶 : - `How is logging configured in this project?` - `What's the pattern for adding a new API endpoint?` - `Explain the authentication flow` - `Where are the database migrations?` ### [试验驱动的发展](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#test-driven-development>) 与副驾驶CLI对等,以开发测试. - `Write failing tests for the user registration flow` - *审查和批准测试。* - `Now implement code to make all tests pass` - *审查执行情况。* - `Commit with message "feat: add user registration"` ### [守则审查援助](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#code-review-assistance>) - `/security-review Review my current local changes for security issues. Prioritize high-severity findings and suggest remediations I can apply before opening a pull request.` - ``/review Use Claude Opus 5.5 and GPT-5.3-Codex to review the changes in my current branch against `main`. Focus on potential bugs and security issues.`` - 三进制高精度结果 首先验证你的修补,然后继续通过正常的拉动请求审查工作流程. ### [Git 操作](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#git-operations>) 副驾驶在 Git 工作流程中表现优异 : - ``What changes went into version `2.3.0`?`` - `Create a PR for this branch with a detailed description` - `` Rebase this branch against `main` `` - `` Resolve the merge conflicts in `package.json` `` ### [错误调查](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#bug-investigation>) - ``The `/api/users` endpoint returns 500 errors intermittently. Search the codebase and logs to identify the root cause.`` ### [正在重构](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#refactoring>) - `/plan Migrate all class components to functional components with hooks` 然后回答“副驾驶”提出的问题。请检查它所创建的计划,必要时请“副驾驶”作出修改。当您对该计划满意时,您可以提示: `Implement this plan` ## [6. 高级图案](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#6-advanced-patterns>) ### [跨多个储存库的工作](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#work-across-multiple-repositories>) **副驾驶CLI提供灵活的多存储工作流程**——为从事微服务,单体,或相关项目的团队提供的关键差异. **选项 1: 从父目录运行** ```bash # Navigate to a parent directory containing multiple repos cd ~/projects copilot ``` 副驾驶现在可以同时访问和工作到所有的儿童存放处。 - 微服务架构 - 对相关再投资进行协调一致的改革 - 重新确定各项目的共同模式 **选项 2: 使用 `/add-dir` 扩大使用范围** ```bash # Start in one repo, then add others (requires full paths) copilot /add-dir /Users/me/projects/backend-service /add-dir /Users/me/projects/shared-libs /add-dir /Users/me/projects/documentation ``` **查看和管理允许的目录 :** ```copilot /list-dirs ``` **示例工作流程:协调API更改** ```copilot I need to update the user authentication API. The changes span: - @/Users/me/projects/api-gateway (routing changes) - @/Users/me/projects/auth-service (core logic) - @/Users/me/projects/frontend (client updates) Start by showing me the current auth flow across all three repos. ``` 这种多储存能力使: - 交叉因素(更新各地共享的模式) - API 合同变更及客户端更新 - 引用多个代码库的文档 - 单雷波的依赖性升级 ### [使用图像进行 UI 工作](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#using-images-for-ui-work>) 副驾驶可以做视觉参考。 **拖放( D)** 图像直接输入到 CLI 输入中, 使用 Ctrl+V 从剪贴板粘贴一个图像, 或在您的提示中引用图像文件 : ```copilot Implement this design: @mockup.png Match the layout and spacing exactly ``` ### [复杂迁移的核对表](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#checklists-for-complex-migrations>) 大规模更改: ```copilot Run the linter and write all errors to `migration-checklist.md` as a checklist. Then fix each issue one by one, checking them off as you go. ``` ### [自动任务完成](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#autonomous-task-completion>) 切换到自动驾驶模式, 使 Copilot 能够自主地对任务进行工作, 直到任务完成为止。 对于不需要经常监管的长期任务, 这是理想的 。 更多信息请参见 。 [使用自动驾驶模式GitHub副驾驶CLI](<https://docs.github.com/en/copilot/how-tos/copilot-cli/use-copilot-cli/use-autopilot-mode>). 选择时,您通常可以通过使用 `/fleet` 在您提示的起始处斜线命令允许 Copilot 将任务突破为由子代理运行的并行子任务 。 更多信息请参见 [以/fleet 命令加速任务完成](<https://docs.github.com/en/copilot/how-tos/copilot-cli/use-copilot-cli/speed-up-task-completion>). ## [7. 团队准则](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#7-team-guidelines>) ### [建议的仓库设置](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#recommended-repository-setup>) - **创建 `.github/copilot-instructions.md`** 改为: - 构建和测试命令 - 代码样式准则 - 实施前必须进行的检查 - 结构决定 - **制定公约** 用于: - 何时使用 `/plan` (复杂特征,重构). - 何时使用 `/delegate` (巡回工作) - 在AI帮助下进行代码审查 ### [安全考虑](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#security-considerations>) - 在工作开始前审查您的许可模式和工具权限。 自动批准可以允许潜在的破坏性操作, 而无需单个提示 。 - 在接受所有拟议修改之前审查所有修改。 - 明智地使用许可列表 。 - 永远不要做出秘密,副驾驶是为了避免这个,但总是核实。 ### [衡量生产率](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#measuring-productivity>) 轨迹衡量标准如下: - 从发行到拉动请求的时间 - 合并前的重复次数 - 守则审查反馈周期 - 提高测试覆盖率 ## [获得帮助](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#getting-help>) 从命令行中,您可以通过使用命令显示帮助 : `copilot -h`. 各个主题的帮助输入 : ```bash copilot help TOPIC ``` 地点 `TOPIC` 可以是以下之一: `config`, `commands`, `environment`, `logging`,或 `permissions`. ### [在CLI范围内](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#within-the-cli>) 如需在 CLI 中提供帮助,请输入: ```copilot /help ``` 要查看使用统计,请输入: ```copilot /usage ``` 要向 GitHub 提交有关 Copilot CLI 的私人反馈,提出错误报告,或提交特性请求,请输入: ```copilot /feedback ``` ## [亲手练习](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#hands-on-practice>) 试试看 [使用副驾驶 CLI 创建应用程序](<https://github.com/skills/create-applications-with-the-copilot-cli>) 与副驾驶CLI一起进行实践体验技能练习,构建应用程序. 下面是你们要学的: - 安装副驾驶 CLI - 使用问题模板创建问题 - 生成一个节点.js CLI计算器应用 - 扩展计算功能 - 计算函数的写单位测试 - 创建、审查和合并您的拉动请求 ## [进一步阅读](<https://docs.github.com/en/copilot/how-tos/copilot-cli/cli-best-practices#further-reading>) - [GitHub 副驾驶 CLI](<https://docs.github.com/en/copilot/concepts/copilot-surfaces/copilot-cli>) - [使用 GitHub 副驾驶 CLI](<https://docs.github.com/en/copilot/how-tos/copilot-cli/use-copilot-cli/overview>) - [GitHub 副驾驶 CLI 命令参考](<https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-command-reference>) - [副驾驶计划和定价](<https://github.com/features/copilot/plans>)
查看原始来源