如何自定义 GitHub Copilot 的响应方式

发布: (2025年12月4日 GMT+8 10:34)
5 min read
原文: Dev.to

Source: Dev.to

你经常在使用 GitHub Copilot 吗?每次聊天都要重新输入相同或相似的指令会很繁琐。自定义指令让你可以保存可复用的指导——比如项目特定的政策、命名约定或注释标准——这样 Copilot 能自动应用它们。

自定义 GitHub Copilot 对提示的响应方式主要有两种:

  • 自定义指令
  • 提示文件(本文未涉及)

自定义指令

自定义指令用于引导 GitHub Copilot 的行为。撰写本文时,可用的作用域包括:

  • 个人自定义指令
  • 仓库级自定义指令
  • 路径特定自定义指令
  • 组织自定义指令(Copilot Enterprise)
  • 代理指令(用于 AI 代理)

个人自定义指令

个人指令在网页端的 Copilot 聊天 UI 中设置,影响 Copilot 对你的提示的解释方式。

GitHub Copilot chat on the web

示例工作流

  1. 打开 GitHub Copilot on the web

  2. 向 Copilot 提示,例如 Create a JavaScript function that converts numbers into Roman numerals

  3. 观察输出。

  4. 在 Copilot 设置中打开 Profile → Personal instructions,添加如下指导:

    Always do the following when writing functions:
    - Add descriptive comments
    - Add input validation
    - Use early returns for error conditions
    - Use meaningful variable names
    - Include at least one example usage in comments
  5. 重新运行相同的提示并比较结果。

阅读 GitHub 文档中关于个人自定义指令的更多信息

仓库级自定义指令

仓库级指令确保每位贡献者获得相同的指导——这对共享框架、编码标准或注释约定非常有用。

  1. 在仓库的 .github 目录下创建名为 copilot-instructions.md 的文件。

  2. 用 Markdown 编写自然语言的指导,例如:

    # Project Overview
    
    This project [describe your project].
    
    ## Folder Structure
    
    [list the folder structure]
    
    ## Libraries and Frameworks
    
    [list any libraries it uses]
    
    ## Coding Standards
    
    [list your coding standards]
    
    ## UI Guidelines
    
    [list your UI guidelines]

    默认情况下,仓库指令是启用的。

Copilot Code review custom instructions

使用 Copilot 生成文件

通过 Copilot Agents UI

  1. 访问 GitHub Copilot Agents page 并选择你的仓库。

  2. 向代理发送类似以下的请求:

    Your task is to add a .github/copilot-instructions.md file to the repository.
    
    [outline the goals you want GitHub Copilot to achieve]
    
    [add limitations such as length or task‑specific constraints]
    
    Add high‑level details about the codebase:
    - A summary of what the repository does.
    - Repository size, primary languages, frameworks, target runtimes.
    
    Include build and validation instructions:
    [insert build steps]
    
    List key architectural facts:
    [describe project layout and architecture]
    
    [describe the steps Copilot should take]
  3. 审核生成的 copilot-instructions.md 并根据需要进行调整。

通过 VS Code

  1. 打开目标仓库。
  2. 在 Copilot 聊天面板中,点击 Settings(齿轮)图标并选择 Generate instructions
  3. 检查生成的 .github/copilot-instructions.md 文件并提交。

阅读 GitHub 文档中关于仓库级自定义指令的更多信息

路径特定自定义指令

路径特定指令仅对选定的文件或模式生效,提供细粒度的控制。目前在 VS Code 和 JetBrains IDE 中受支持。

  1. 在仓库内部创建文件夹 .github/instructions

  2. 添加一个名为 .instructions.md 的 Markdown 文件(使用描述性的名称)。

  3. 在第一行使用 applyTo 键指定目标文件,例如:

    applyTo: "app/models/**/*.rb"

    多个模式可用逗号分隔:

    applyTo: "**/*.ts,**/*.tsx"

    若要应用于所有文件:

    applyTo: "**"
  4. applyTo 行下方使用普通 Markdown 编写针对这些文件的指导内容。

你可以根据需要创建任意数量的路径特定指令文件,每个文件针对代码库的不同子集。

Back to Blog

相关文章

阅读更多 »

🌑 进入黑暗:Soulbound Codex

演示图片 https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2...