AI 编程教程中文版
官方教程中文版补全与 Chat

响应定制

说明 GitHub Copilot custom instructions、prompt files、AGENTS.md 类文件和优先级如何影响回答。

响应定制(response customization)解决的是“每次都要重新解释项目约定”的问题。GitHub 官方文档把它描述为给 Copilot 提供个人偏好、团队工作方式、工具和项目上下文,让回答更贴合真实环境。

定制不是越多越好。它会随每次相关请求进入上下文,应该只写稳定、广泛适用、不会泄露敏感信息的规则。

阅读目标:读完本章,你应该能区分 personal、repository、organization、path-specific 和 agent instructions,并知道它们的优先级。

1. 定制类型

官方文档列出几类常用定制方式:

  • Personal instructions:个人偏好,适用于 GitHub.com 上的 Copilot Chat。
  • Repository-wide custom instructions:仓库级规则,写在 .github/copilot-instructions.md
  • Path-specific instructions:路径级规则,写在 .github/instructions/ 下的 *.instructions.md 文件。
  • Agent instructions:类似仓库规则,但不是所有 Copilot 功能都支持;文件名可以是 AGENTS.mdCLAUDE.mdGEMINI.md
  • Organization custom instructions:组织级规则,由 organization owner 设置,面向 Copilot Business 或 Copilot Enterprise 订阅。
  • Prompt files:工作区里的 Markdown prompt 文件,用于复用某类 Chat 请求。
flowchart TD
    Request["Copilot request"] --> Personal["Personal instructions"]
    Request --> Repo["Repository instructions"]
    Repo --> Path[".github/instructions/*.instructions.md"]
    Repo --> Wide[".github/copilot-instructions.md"]
    Repo --> Agent["AGENTS.md / CLAUDE.md / GEMINI.md"]
    Request --> Org["Organization instructions"]
    Request --> Prompt["Prompt file"]
    Personal --> Response["Customized response"]
    Path --> Response
    Wide --> Response
    Agent --> Response
    Org --> Response
    Prompt --> Response

    style Personal fill:#dbeafe,stroke:#2563eb,stroke-width:2px
    style Repo fill:#fef3c7,stroke:#d97706,stroke-width:2px
    style Response fill:#dcfce7,stroke:#16a34a,stroke-width:2px

2. 优先级

官方页面说明,多种 instructions 可以同时适用于一次请求。优先级从高到低是:

  1. Personal instructions。
  2. Path-specific instructions。
  3. Repository-wide .github/copilot-instructions.md
  4. Agent instructions,例如 AGENTS.md
  5. Organization custom instructions。

注意:优先级高并不代表低优先级完全不生效。相关 instructions 都可能提供给 Copilot;冲突越少,结果越稳定。

3. 什么时候写进 instructions

适合写:

  • 项目架构和目录职责。
  • 稳定技术栈、框架和版本约束。
  • 命名、错误处理、测试风格、lint 规则。
  • 安全红线,例如不能记录 token。
  • 团队固定语言和回答格式偏好。

不适合写:

  • 一次性任务说明。
  • 具体 bug 的临时方案。
  • 密钥、账号、内网地址、客户信息。
  • 过期迁移步骤。
  • 互相冲突的多套规范。

4. 路径级规则更适合复杂仓库

大型仓库不要把所有内容塞进一个仓库级文件。官方文档说明,path-specific instructions 可以避免 repository-wide instructions 过载。

例子:

.github/copilot-instructions.md
.github/instructions/frontend.instructions.md
.github/instructions/backend.instructions.md
.github/instructions/tests.instructions.md

这样前端、后端和测试可以有不同要求,Copilot 不需要每次都读无关规则。

5. Organization instructions 的边界

官方页面说明,organization custom instructions 当前主要支持 GitHub.com 上的 Copilot Chat、Copilot code review 和 Copilot cloud agent。它适合统一组织偏好,例如语言、安全知识库、回答风格。

组织级规则不应该替代仓库规则。组织级写原则,仓库级写项目事实,路径级写局部约束。

6. 写法原则

有效 instructions 应该短、独立、可长期复用。建议包含:

  • 项目用途和核心目标。
  • 重要目录结构。
  • 编码标准和命名约定。
  • 关键框架、库和版本。
  • 测试、构建和审查要求。

不要写成长篇教程。Copilot code review 对 custom instruction file 还有读取长度限制;官方文档提示 code review 只读取每个 custom instruction file 的前 4,000 字符,这个限制不适用于 Copilot Chat 或 cloud agent。

深读:为什么定制规则会降低也会放大风险

好的 instructions 会减少重复解释,让 Copilot 更贴合项目;坏的 instructions 会把错误规则自动注入所有任务。最危险的是过期规则和冲突规则,因为用户很难在每次回答里察觉它们被使用了。

所以 instructions 需要像代码一样维护:有 owner、有 review、有删除机制,不把一次性经验永久化。

本章自检

完成本章后,用这 4 个问题检查:

  1. 这条规则是个人偏好、仓库事实、路径约束,还是组织原则?
  2. 它是否会反复适用,而不是一次性任务?
  3. 它是否和已有 instructions 冲突?
  4. 它是否包含敏感信息、过期事实或不可验证口号?

通过标准:你能把稳定规则沉淀进正确层级,并把临时任务留在 prompt 或 issue 里。

官方来源

接下来去哪

本页目录