如何自定义 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文件并提交。
路径特定自定义指令
路径特定指令仅对选定的文件或模式生效,提供细粒度的控制。目前在 VS Code 和 JetBrains IDE 中受支持。
-
在仓库内部创建文件夹
.github/instructions。 -
添加一个名为
.instructions.md的 Markdown 文件(使用描述性的名称)。 -
在第一行使用
applyTo键指定目标文件,例如:applyTo: "app/models/**/*.rb"多个模式可用逗号分隔:
applyTo: "**/*.ts,**/*.tsx"若要应用于所有文件:
applyTo: "**" -
在
applyTo行下方使用普通 Markdown 编写针对这些文件的指导内容。
你可以根据需要创建任意数量的路径特定指令文件,每个文件针对代码库的不同子集。