{"meta":{"title":"GitHub Copilot CLI 的最佳做法","intro":"了解如何充分利用 GitHub Copilot 命令行界面 (CLI)。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/how-tos","title":"操作方法"},{"href":"/zh/copilot/how-tos/copilot-cli","title":"Copilot 命令行界面（CLI）"},{"href":"/zh/copilot/how-tos/copilot-cli/cli-best-practices","title":"Copilot CLI 最佳做法"}],"documentType":"article"},"body":"# GitHub Copilot CLI 的最佳做法\n\n了解如何充分利用 GitHub Copilot 命令行界面 (CLI)。\n\n## 介绍\n\n```\n          GitHub Copilot 命令行界面 (CLI) 是一个终端原生 AI 编码助手，用于将代理功能直接引入命令行。 \n          Copilot 命令行界面（CLI） 可以像聊天机器人一样操作，回答你的问题，但它的真正力量在于它能够作为编码合作伙伴自主工作，让你委托任务并监督其工作。\n```\n\n本文提供有关如何充分利用 Copilot 命令行界面（CLI） 的提示，包括如何有效使用各种 CLI 命令以及如何管理 CLI 对文件的访问。 将这些提示视为起点，然后尝试找出最适合工作流的内容。\n\n> \\[!NOTE]\n\n```\n          GitHub Copilot 命令行界面 (CLI) 不断发展。 \n```\n\n```\n          `/help`使用命令查看最新信息。\n```\n\n## 1.自定义环境\n\n### 使用自定义说明文件\n\n```\n          Copilot 命令行界面（CLI） 自动读取来自多个位置的说明，允许你定义组织范围的标准和特定于存储库的约定。\n\n          **支持的位置（按发现顺序）：**\n```\n\n| 位置                                          | Scope    |\n| ------------------------------------------- | -------- |\n| `~/.copilot/copilot-instructions.md`        | 所有会话（全局） |\n| `.github/copilot-instructions.md`           | 资料库      |\n| `.github/instructions/**/*.instructions.md` | 存储库（模块化） |\n| `AGENTS.md` （在 Git 根或 cwd 中）                | 资料库      |\n| `Copilot.md`、`GEMINI.md`、`CODEX.md`         | 资料库      |\n\n#### 最佳做法\n\n存储库说明 **始终优先** 于全局指令。 使用此函数强制实施团队约定。 例如，这是一个简单的 `.github/copilot-instructions.md` 文件。\n\n```markdown\n## Build Commands\n- `npm run build` - Build the project\n- `npm run test` - Run all tests\n- `npm run lint:fix` - Fix linting issues\n\n## Code Style\n- Use TypeScript strict mode\n- Prefer functional components over class components\n- Always add JSDoc comments for public APIs\n\n## Workflow\n- Run `npm run lint:fix && npm test` after making changes\n- Commit messages follow conventional commits format\n- Create feature branches from `main`\n```\n\n> \\[!TIP]\n> 保持说明简洁且可执行。 冗长的指令可以稀释有效性。\n\n有关详细信息，请参阅“[关于自定义GitHub Copilot 响应](/zh/copilot/concepts/prompting/response-customization?tool=webui)”。\n\n### 配置允许的工具\n\n在不请求权限的情况下管理哪些工具 Copilot 可以运行。 请求操作权限时 Copilot ，通常可以选择只允许此操作，或者允许工具用于 CLI 会话的其余部分。\n\n若要重置以前批准的工具，请使用：\n\n```copilot\n/reset-allowed-tools\n```\n\n还可以通过 CLI 标志预配置允许的工具：\n\n```bash\ncopilot --allow-tool='shell(git:*)' --deny-tool='shell(git push)'\n```\n\n```\n          **常见权限模式：**\n```\n\n* `shell(git:*)` — 允许所有 Git 命令\n* `shell(npm run:*)` — 允许所有 npm 脚本\n* `shell(npm run test:*)` — 允许 npm 测试命令\n* `write` — 允许文件写入\n\n### 选择首选模型\n\n用于 `/model` 根据任务复杂性从可用模型中进行选择：\n\n| 型号                        | 最适用于                 | 权衡                                                                                                             |\n| ------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------------- |\n| **自动**                    | 降低速率限制，降低延迟和错误       | 请参阅“[关于 Copilot 自动模型选择](/zh/copilot/concepts/auto-model-selection#auto-model-selection-in-github-copilot-cli)” |\n| **Claude Opus 4.5** （默认值） | 复杂的体系结构，难以调试，细微差别的重构 | 最有能力但使用更多 [高级请求](/zh/copilot/concepts/billing/copilot-requests#model-multipliers)                              |\n| **克劳德十四行诗 4.5**           | 日常编码，大多数常规任务         | 快捷、经济适用、能够很好地处理大部分工作                                                                                           |\n| **GPT-5.2 Codex**         | 代码生成、代码评审、直接实现       | 非常适用于查看其他模型生成的代码                                                                                               |\n\n```\n          **Recommendations:**\n```\n\n* **根据** 实时系统运行状况和模型性能自动选择模型，降低速率限制并提供较低的延迟和错误。\n* **Opus 4.5** 非常适合需要深度推理、复杂的系统设计、微妙的 bug 调查或广泛的上下文理解的任务。\n* **切换到 Sonnet 4.5** 以执行速度与成本效益很重要的常规任务，它可以有效地处理大多数日常编码。\n* **使用 Codex** 生成大量代码，并作为查看其他模型生成的代码的第二种意见。\n\n你可以在会话过程中根据任务复杂性的变化切换 `/model` 模型。\n\n如果组织或企业已使用自己的 LLM 提供程序 API 密钥配置自定义模型，这些模型也会显示在 `/model` 列表底部。\n\n### 使用自己的模型提供程序\n\n可以配置 Copilot 命令行界面（CLI） 使用您自己的模型提供程序，而不是使用 GitHub 托管的模型。 运行 `copilot help providers` 以获取完整的安装说明。\n\n```\n          **关键注意事项：**\n```\n\n* 模型必须支持 **工具调用** （函数调用）和 **流式处理**。\n  Copilot 命令行界面（CLI） 如果缺少任一功能，则返回错误。\n* 为了获得最佳结果，请使用具有至少 128k 个标记的上下文窗口的模型。\n* 内置子代理（`/review`、`/task`、`/fleet`）会自动继承您的提供程序配置。\n* 使用你自己的提供程序时，高级请求成本估算将会隐藏。 令牌使用情况（输入、输出和缓存计数）仍显示。\n* 只有在您也登录到`/delegate`的情况下，GitHub才有效。 它将会话传输到GitHub的服务器端Copilot，而不是你的提供商。\n\n请参阅 [使用自己的模型提供程序](/zh/copilot/concepts/agents/copilot-cli/about-copilot-cli#using-your-own-model-provider)。\n\n## 2.在编码之前规划\n\n### 计划模式\n\n```\n          **根据具体的计划，模型可以达到更高的成功率。** 在计划模式下， Copilot 将在编写任何代码之前创建结构化实现计划。\n```\n\n按 <kbd>Shift</kbd>+<kbd>Tab</kbd> 在正常模式和计划模式之间切换。 在计划模式下，输入的所有提示都会触发计划工作流。\n\n或者，可以在正常模式下使用 `/plan` 命令来实现相同的效果。\n\n```\n          **示例提示（从正常模式）：**\n```\n\n```copilot\n/plan Add OAuth2 authentication with Google and GitHub providers\n```\n\n```\n          **发生的情况：**\n```\n\n* Copilot 分析请求和代码库。\n* **提出澄清问题** ，以符合要求和方法。\n* 使用复选框创建结构化实现计划。\n* 将计划保存到 `plan.md` 会话文件夹中。\n* 在实施之前**等待审批**。\n\n可以 <kbd>按 Ctrl</kbd>+<kbd>y</kbd> 在 Markdown 文件的默认编辑器中查看和编辑计划。\n\n```\n          **计划输出示例：**\n```\n\n```markdown\n# Implementation Plan: OAuth2 Authentication\n\n## Overview\nAdd social authentication using OAuth2 with Google and GitHub providers.\n\n## Tasks\n- [ ] Install dependencies (passport, passport-google-oauth20, passport-github2)\n- [ ] Create authentication routes in `/api/auth`\n- [ ] Implement passport strategies for each provider\n- [ ] Add session management middleware\n- [ ] Create login/logout UI components\n- [ ] Add environment variables for OAuth credentials\n- [ ] Write integration tests\n\n## Detailed Steps\n1. **Dependencies**: Add to package.json...\n2. **Routes**: Create `/api/auth/google` and `/api/auth/github`...\n```\n\n### 何时使用计划模式\n\n| Scenario   | 使用计划模式？                                                                                                                                                                                                                                                                                                                                                                                                                  |\n| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| 复杂的多文件更改   | <svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-check\" aria-label=\"Yes\" role=\"img\"><path d=\"M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z\"></path></svg>                                                                                                       |\n| 涉及多个接触点的重构 | <svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-check\" aria-label=\"Yes\" role=\"img\"><path d=\"M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z\"></path></svg>                                                                                                       |\n| 新功能实现      | <svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-check\" aria-label=\"Yes\" role=\"img\"><path d=\"M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z\"></path></svg>                                                                                                       |\n| 快速修复漏洞     | <svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-x\" aria-label=\"No\" role=\"img\"><path d=\"M3.72 3.72a.75.75 0 0 1 1.06 0L8 6.94l3.22-3.22a.749.749 0 0 1 1.275.326.749.749 0 0 1-.215.734L9.06 8l3.22 3.22a.749.749 0 0 1-.326 1.275.749.749 0 0 1-.734-.215L8 9.06l-3.22 3.22a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042L6.94 8 3.72 4.78a.75.75 0 0 1 0-1.06Z\"></path></svg> |\n| 单个文件更改     | <svg version=\"1.1\" width=\"16\" height=\"16\" viewBox=\"0 0 16 16\" class=\"octicon octicon-x\" aria-label=\"No\" role=\"img\"><path d=\"M3.72 3.72a.75.75 0 0 1 1.06 0L8 6.94l3.22-3.22a.749.749 0 0 1 1.275.326.749.749 0 0 1-.215.734L9.06 8l3.22 3.22a.749.749 0 0 1-.326 1.275.749.749 0 0 1-.734-.215L8 9.06l-3.22 3.22a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042L6.94 8 3.72 4.78a.75.75 0 0 1 0-1.06Z\"></path></svg> |\n\n### 探索→计划→代码→提交工作流\n\n为获得最佳复杂任务结果：\n\n* **浏览**：\n\n  `Read the authentication files but don't write code yet`\n\n* **计划**：\n\n  `/plan Implement password reset flow`\n\n* **审查**：\n\n  检查计划，建议修改\n\n* **实现**：\n\n  `Proceed with the plan`\n\n* **验证**：\n\n  `Run the tests and fix any failures`\n\n* **提交**：\n\n  `Commit these changes with a descriptive message`\n\n## 3. 利用无限会话\n\n### 自动上下文窗口管理\n\n```\n          Copilot 命令行界面（CLI） 具有 **无限会话**。 你无需担心上下文耗尽。 系统通过智能压缩自动管理上下文，以汇总对话历史记录，同时保留基本信息。\n\n          **会话存储位置：**\n```\n\n```text\n~/.copilot/session-state/{session-id}/\n├── events.jsonl      # Full session history\n├── workspace.yaml    # Metadata\n├── plan.md           # Implementation plan (if created)\n├── checkpoints/      # Compaction history\n└── files/            # Persistent artifacts\n```\n\n> \\[!NOTE]\n> 如果需要手动触发压缩，请使用 `/compact`。 这很少是必需的，因为系统会自动处理它。\n\n### 会话管理命令\n\n若要查看有关当前 CLI 会话的信息，请输入：\n\n```copilot\n/session\n```\n\n若要查看任何会话检查点的列表，请输入：\n\n```copilot\n/session checkpoints\n```\n\n> \\[!NOTE]\n> 压缩会话上下文时会创建检查点，并允许查看创建的 Copilot 摘要上下文。\n\n若要查看特定检查点的详细信息，请输入：\n\n```copilot\n/session checkpoints NUMBER\n```\n\n其中 NUMBER 指定要显示的检查点。\n\n若要查看在当前会话期间创建的任何临时文件（例如，由 Copilot 创建但不应保存到存储库的工件），请输入：\n\n```copilot\n/session files\n```\n\n若要查看当前计划（如果 Copilot 已生成计划），请输入：\n\n```copilot\n/session plan\n```\n\n### 最佳做法：使会议保持专注\n\n虽然无限会话允许长时间运行的工作，但专注的会话会产生更好的结果：\n\n* 使用 `/clear` 或 `/new` 在不相关的任务之间。\n* 这会重置上下文并提高响应质量。\n* 想想就像开始与同事进行新的对话。\n\n###\n\n```\n          `/context` 命令\n```\n\n使用 `/context`. 可视化当前上下文使用情况。 它会显示以下内容的细分：\n\n* 系统/工具令牌\n* 消息历史记录令牌\n* 可用空间\n* 缓冲区分配\n\n## 4. 有效地分配工作\n\n###\n\n```\n          `/delegate` 命令\n\n          **使用 Copilot云代理 将工作卸载到云中运行。** 这在以下方面特别有效：\n```\n\n* 可以异步运行的任务。\n* 对其他存储库的更改。\n* 你不想等待的长时间运行的操作。\n\n  ```\n          **示例提示词：**\n  ```\n\n```copilot\n/delegate Add dark mode support to the settings page\n```\n\n```\n          **发生的情况：**\n```\n\n* 你的请求将发送到 Copilot云代理。\n* 智能体会创建一个包含更改的拉取请求。\n* 可以在云代理工作期间继续在本地工作。\n\n### 何时使用 `/delegate`\n\n| 使用 `/delegate` | 在本地工作  |\n| -------------- | ------ |\n| 附带任务           | 核心功能开发 |\n| 文档更新           | 调试     |\n| 重构单独的模块        | 交互式探索  |\n\n## 5. 常见工作流\n\n### 代码库入门\n\n在加入新项目时，将 Copilot 命令行界面（CLI） 用作您的配对编程合作伙伴。 例如，可以询问 Copilot：\n\n* `How is logging configured in this project?`\n* `What's the pattern for adding a new API endpoint?`\n* `Explain the authentication flow`\n* `Where are the database migrations?`\n\n### 测试驱动开发\n\n将 Copilot 命令行界面（CLI） 与之配对以开发测试。\n\n* `Write failing tests for the user registration flow`\n* *查看和批准测试。*\n* `Now implement code to make all tests pass`\n* *查看实现。*\n* `Commit with message \"feat: add user registration\"`\n\n### 代码评审协助\n\n* ``/review Use Opus 4.5 and Codex 5.2 to review the changes in my current branch against `main`. Focus on potential bugs and security issues.``\n\n### Git 操作\n\n```\n          Copilot 在 Git 工作流中表现出色：\n```\n\n* ``What changes went into version `2.3.0`?``\n* `Create a PR for this branch with a detailed description`\n* ``Rebase this branch against `main` ``\n* ``Resolve the merge conflicts in `package.json` ``\n\n### Bug 调查\n\n* ``The `/api/users` endpoint returns 500 errors intermittently. Search the codebase and logs to identify the root cause.``\n\n### 重构\n\n* `/plan Migrate all class components to functional components with hooks`\n\n  然后回答Copilot提出的问题。 查看它创建的计划，并在必要时要求 Copilot 进行更改。 当你对计划感到满意时，可以提示： `Implement this plan`\n\n## 6. 高级模式\n\n### 在多个存储库中工作\n\n```\n          **\n          Copilot 命令行界面（CLI） 提供灵活的多存储库工作流**，这是处理微服务、monorepos 或相关项目的团队的主要区别。\n\n          **选项 1：从父目录运行**\n```\n\n```bash\n# Navigate to a parent directory containing multiple repos\ncd ~/projects\ncopilot\n```\n\n```\n          Copilot 现在可以同时访问和处理所有子存储库。 这非常适合用于：\n```\n\n* 微服务体系结构\n* 在相关存储库间进行协调更改\n* 跨项目重构共享模式\n\n  ```\n          **选项 2：用于 `/add-dir` 扩展访问权限**\n  ```\n\n```bash\n# Start in one repo, then add others (requires full paths)\ncopilot\n/add-dir /Users/me/projects/backend-service\n/add-dir /Users/me/projects/shared-libs\n/add-dir /Users/me/projects/documentation\n```\n\n```\n          **查看和管理允许的目录：**\n```\n\n```copilot\n/list-dirs\n```\n\n```\n          **示例工作流：协调的 API 更改**\n```\n\n```copilot\nI need to update the user authentication API. The changes span:\n\n- @/Users/me/projects/api-gateway (routing changes)\n- @/Users/me/projects/auth-service (core logic)\n- @/Users/me/projects/frontend (client updates)\n\nStart by showing me the current auth flow across all three repos.\n```\n\n此多存储库能力使能：\n\n* 跨领域重构（在所有地方更新共享模式）\n* 客户端更新引起的 API 合同更改\n* 引用多个代码库的文档\n* 跨 monorepo 的依赖项升级\n\n### 使用图像处理 UI 工作\n\n```\n          Copilot 可以处理视觉参考。 只需将图像直接**拖放**到 CLI 输入中，或引用图像文件：\n```\n\n```copilot\nImplement this design: @mockup.png\nMatch the layout and spacing exactly\n```\n\n### 复杂迁移的清单\n\n对于大规模更改：\n\n```copilot\nRun the linter and write all errors to `migration-checklist.md` as a checklist.\nThen fix each issue one by one, checking them off as you go.\n```\n\n### 自主任务完成\n\n切换到 Autopilot 模式，以允许 Copilot 在任务完成之前自主工作。 这非常适合不需要持续监督的长时间运行的任务。 有关详细信息，请参阅“[允许 GitHub Copilot CLI 自主工作](/zh/copilot/concepts/agents/copilot-cli/autopilot)”。\n\n或者，通常可以通过在提示开始时使用 `/fleet` 斜杠命令来加快大型任务的速度，以允许 Copilot 将任务分解为子代理运行的并行子任务。 有关详细信息，请参阅“[使用 \\`/fleet\\` 命令并行运行任务](/zh/copilot/concepts/agents/copilot-cli/fleet)”。\n\n## 7. 团队指南\n\n### 建议的存储库设置\n\n* **使用 `.github/copilot-instructions.md`** 创建：\n  * 生成和测试命令\n  * 代码样式指南\n  * 提交前的必要检查\n  * 体系结构决策\n\n* **建立规范**：\n  * 何时使用 `/plan` （复杂功能，重构）\n  * 何时使用 `/delegate`（相关工作）\n  * 通过 AI 协助进行代码评审\n\n### 安全注意事项\n\n* Copilot 命令行界面（CLI） 需要明确批准潜在的破坏性操作。\n* 在接受之前，请查看所有建议的更改。\n* 谨慎使用权限允许列表。\n* 从不提交机密信息。\n  Copilot 旨在避免这种情况，但要始终进行验证。\n\n### 测量工作效率\n\n可以跟踪的指标包括：\n\n* 从问题到拉取请求的时间\n* 合并前的迭代次数\n* 代码评审反馈周期\n* 测试覆盖率改进\n\n## 获取帮助\n\n在命令行中，可以使用以下命令显示帮助。 `copilot -h`\n\n有关各种主题的帮助，请输入：\n\n```bash\ncopilot help TOPIC\n```\n\n其中`TOPIC`可以是：`config`、、`commands``environment`、或`logging``permissions`。\n\n### 在命令行界面 (CLI) 中\n\n有关 CLI 中的帮助，请输入：\n\n```copilot\n/help\n```\n\n若要查看使用情况统计信息，请输入：\n\n```copilot\n/usage\n```\n\n若要将私人反馈提交到 GitHub 有关 Copilot 命令行界面（CLI）、提出 bug 报告或提交功能请求，请输入：\n\n```copilot\n/feedback\n```\n\n## 动手练习\n\n尝试[使用 Copilot 命令行界面（CLI） 技能创建应用程序](https://github.com/skills/create-applications-with-the-copilot-cli)练习，以获得使用 Copilot 命令行界面（CLI） 构建应用程序的实践经验。\n\n以下是你将了解的内容：\n\n* 安装 Copilot 命令行界面（CLI）\n* 使用问题模板创建问题\n* 生成 Node.js CLI 计算器应用\n* 扩展计算器功能\n* 编写计算器函数的单元测试\n* 创建、审核和合并你的提交请求\n\n## 延伸阅读\n\n* [关于 GitHub Copilot CLI](/zh/copilot/concepts/agents/about-copilot-cli)\n* [使用 GitHub Copilot CLI](/zh/copilot/how-tos/use-copilot-agents/use-copilot-cli)\n* [GitHub Copilot CLI 命令参考](/zh/copilot/reference/copilot-cli-reference/cli-command-reference)\n* [\n  Copilot 计划和定价](https://github.com/features/copilot/plans)"}