如何自定义 GitHub Copilot 的响应方式
Source: Dev.to
你经常在使用 GitHub Copilot 吗?每次聊天都要重新输入相同或相似的指令会很繁琐。自定义指令让你可以保存可重复使用的指导——例如项目特定的政策、命名约定或注释标准——这样 Copilot 就能自动应用它们。
有两种主要方式来自定义 GitHub Copilot 对提示的响应:
- 自定义指令
- 提示文件(本文未涉及)
自定义指令
自定义指令用于指导 GitHub Copilot 的行为。在撰写本文时,可用的范围包括:
- 个人自定义指令
- 仓库范围自定义指令
- 路径特定自定义指令
- 组织自定义指令(Copilot Enterprise)
- 代理指令(AI 代理使用)
个人自定义指令
个人指令在网页端的 Copilot 聊天 UI 中设置,会影响 Copilot 对你的提示的解释。

示例工作流程
-
向 Copilot 发出提示,例如
Create a JavaScript function that converts numbers into Roman numerals。 -
观察输出。
-
在 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 -
重新运行相同的提示并比较结果。
仓库范围自定义指令
仓库级别的指令确保每位贡献者获得相同的指导——适用于共享框架、编码标准或注释约定。
-
在仓库的
.github目录下创建名为copilot-instructions.md的文件。 -
使用 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 生成文件
通过 Copilot Agents UI
-
访问 GitHub Copilot Agents page 并选择你的仓库。
-
向代理发送如下请求,例如:
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] -
审查生成的
copilot-instructions.md并根据需要进行调整。
通过 VS Code
- 打开目标仓库。
- 在 Copilot 聊天面板中,点击 Settings(齿轮)图标并选择 Generate instructions。
- 检查生成的
.github/copilot-instructions.md文件并提交。
在 GitHub 文档中了解更多关于仓库范围自定义指令的信息.
路径特定自定义指令
路径特定指令仅适用于选定的文件或模式,提供细粒度的控制。目前在 VS Code 和 JetBrains 中受支持.
IDEs。
-
在仓库内部创建一个文件夹
.github/instructions。 -
添加一个 Markdown 文件(例如
.instructions.md),并使用描述性的名称。 -
在第一行使用
applyTo键指定目标文件,例如:applyTo: "app/models/**/*.rb"多个模式可以用逗号分隔:
applyTo: "**/*.ts,**/*.tsx"若要应用于所有文件:
applyTo: "**" -
在
applyTo行下面使用普通的 Markdown 编写针对这些文件的指导说明。
你可以根据需要创建任意数量的路径‑特定指令文件,每个文件针对代码库中的不同子集。