Skip to main content

为GitHub Copilot添加存储库自定义说明

创建存储库自定义说明文件,以提供有关 Copilot 如何了解项目以及如何生成、测试和验证其更改的其他上下文。

Introduction

存储库自定义说明允许您为 Copilot 提供特定的指南和首选项 GitHub。 若要了解如何在 IDE 中设置自定义说明,请参阅 在 IDE 中添加 GitHub Copilot 的存储库自定义说明。 有关自定义说明的详细信息,请参阅 关于自定义GitHub Copilot 响应

仓库自定义指令的先决条件

  • 必须有自定义指令文件(请参阅以下指令)。

  • 对于 Copilot 代码评审,是否要使用自定义说明的个人选择必须设置为启用。 此项已默认启用。 请参阅本文后面的启用或禁用存储库自定义说明

创建自定义指令

          Copilot on GitHub 支持三种类型的存储库自定义指令。 有关哪些 GitHub Copilot 功能支持这些类型的说明的详细信息,请参阅 [AUTOTITLE](/copilot/concepts/prompting/response-customization?tool=webui#support-for-repository-custom-instructions)。
  • 存储库级自定义指令适用于在存储库上下文中发出的所有请求。

    这些在存储库的 copilot-instructions.md 目录中的 .github 文件中指定。 请参阅创建全仓库内的自定义指令

  • 路径特定自定义指令适用于在与指定路径匹配的文件上下文中发出的请求。

    这些值在存储库中NAME.instructions.md目录内或其下的一个或多个.github/instructions文件中指定。 请参阅创建路径特定的自定义指令

    如果指定的路径与正在处理的文件 Copilot 匹配,并且还存在存储库范围的自定义指令文件,则使用这两个文件中的说明。

  • 智能体指令供 AI 智能体使用。

    你可以创建一个或多个 AGENTS.md 文件,存储在仓库内的任意位置。 当 Copilot 工作时,目录树中最近的 AGENTS.md 文件将优先。 有关详细信息,请参阅 agentsmd/agents.md 存储库

    或者,可以使用仓库根目录中存储的单个 CLAUDE.mdGEMINI.md 文件。

创建全仓库内的自定义指令

你可以从头开始创建自己的自定义指令文件。 请参阅编写自己的 copilot-instructions.md 文件。 或者,你可以要求 Copilot云代理 为你生成一个。

          Copilot云代理要求生成`copilot-instructions.md`文件
  1. 转到 github.com/copilot/agents 的“代理”选项卡。

    还可以单击 任何页面上 GitHub搜索栏旁边的按钮,然后从边栏中选择 “代理 ”来访问此页面。

  2. 在提示字段下拉列表中,选择要为其生成自定义说明的存储库 Copilot 。

  3. 复制以下提示并将其粘贴到提示字段中,根据需要对其进行自定义:

    Markdown
    Your task is to "onboard" this repository to Copilot cloud agent by adding a .github/copilot-instructions.md file in the repository that contains information describing how a cloud agent seeing it for the first time can work most efficiently.
    
    You will do this task only one time per repository and doing a good job can SIGNIFICANTLY improve the quality of the agent's work, so take your time, think carefully, and search thoroughly before writing the instructions.
    
    <Goals>
    - Reduce the likelihood of a cloud agent pull request getting rejected by the user due to
    generating code that fails the continuous integration build, fails a validation pipeline, or
    having misbehavior.
    - Minimize bash command and build failures.
    - Allow the agent to complete its task more quickly by minimizing the need for exploration using grep, find, str_replace_editor, and code search tools.
    </Goals>
    
    <Limitations>
    - Instructions must be no longer than 2 pages.
    - Instructions must not be task specific.
    </Limitations>
    
    <WhatToAdd>
    
    Add the following high level details about the codebase to reduce the amount of searching the agent has to do to understand the codebase each time:
    <HighLevelDetails>
    
    - A summary of what the repository does.
    - High level repository information, such as the size of the repo, the type of the project, the languages, frameworks, or target runtimes in use.
    </HighLevelDetails>
    
    Add information about how to build and validate changes so the agent does not need to search and find it each time.
    <BuildInstructions>
    
    - For each of bootstrap, build, test, run, lint, and any other scripted step, document the sequence of steps to take to run it successfully as well as the versions of any runtime or build tools used.
    - Each command should be validated by running it to ensure that it works correctly as well as any preconditions and postconditions.
    - Try cleaning the repo and environment and running commands in different orders and document errors and misbehavior observed as well as any steps used to mitigate the problem.
    - Run the tests and document the order of steps required to run the tests.
    - Make a change to the codebase. Document any unexpected build issues as well as the workarounds.
    - Document environment setup steps that seem optional but that you have validated are actually required.
    - Document the time required for commands that failed due to timing out.
    - When you find a sequence of commands that work for a particular purpose, document them in detail.
    - Use language to indicate when something should always be done. For example: "always run npm install before building".
    - Record any validation steps from documentation.
    </BuildInstructions>
    
    List key facts about the layout and architecture of the codebase to help the agent find where to make changes with minimal searching.
    <ProjectLayout>
    
    - A description of the major architectural elements of the project, including the relative paths to the main project files, the location
    of configuration files for linting, compilation, testing, and preferences.
    - A description of the checks run prior to check in, including any GitHub workflows, continuous integration builds, or other validation pipelines.
    - Document the steps so that the agent can replicate these itself.
    - Any explicit validation steps that the agent can consider to have further confidence in its changes.
    - Dependencies that aren't obvious from the layout or file structure.
    - Finally, fill in any remaining space with detailed lists of the following, in order of priority: the list of files in the repo root, the
    contents of the README, the contents of any key source files, the list of files in the next level down of directories, giving priority to the more structurally important and snippets of code from key source files, such as the one containing the main method.
    </ProjectLayout>
    </WhatToAdd>
    
    <StepsToFollow>
    - Perform a comprehensive inventory of the codebase. Search for and view:
    - README.md, CONTRIBUTING.md, and all other documentation files.
    - Search the codebase for build steps and indications of workarounds like 'HACK', 'TODO', etc.
    - All scripts, particularly those pertaining to build and repo or environment setup.
    - All build and actions pipelines.
    - All project files.
    - All configuration and linting files.
    - For each file:
    - think: are the contents or the existence of the file information that the cloud agent will need to implement, build, test, validate, or demo a code change?
    - If yes:
       - Document the command or information in detail.
       - Explicitly indicate which commands work and which do not and the order in which commands should be run.
       - Document any errors encountered as well as the steps taken to workaround them.
    - Document any other steps or information that the agent can use to reduce time spent exploring or trying and failing to run bash commands.
    - Finally, explicitly instruct the agent to trust the instructions and only perform a search if the information in the instructions is incomplete or found to be in error.
    </StepsToFollow>
       - Document any errors encountered as well as the steps taken to work-around them.
    
    
  4. Click or press Enter.

Copilot will start a new session, which will appear in the list below the prompt box. Copilot will create a draft pull request, write your custom instructions, push them to the branch, then add you as a reviewer when finished, triggering a notification.

Writing your own copilot-instructions.md file

  1. In the root of your repository, create a file named .github/copilot-instructions.md.

    Create the .github directory if it does not already exist.

  2. Add natural language instructions to the file, in Markdown format.

    Whitespace between instructions is ignored, so the instructions can be written as a single paragraph, each on a new line, or separated by blank lines for legibility.

提示

The first time you create a pull request in a given repository with Copilot云代理, Copilot will leave a comment with a link to automatically generate custom instructions for the repository.

Creating path-specific custom instructions

注意

Currently, on GitHub.com, path-specific custom instructions are only supported for Copilot云代理 and Copilot 代码评审.

  1. 如果尚无 .github/instructions 目录,则创建该目录。

  2. (可选)创建用于组织指令文件的子目录 .github/instructions

  3. 创建一个或多个 NAME.instructions.md 文件,其中 NAME 指示指令的用途。 文件名必须以 .instructions.md 结尾。

  4. 在文件开头,创建包含 applyTo 关键字的前辅文块。 使用 glob 语法指定指令应用于的文件或目录。

    例如:

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

    可以通过用逗号分隔多个模式来指定这些模式。 例如,若要将指令应用于仓库中的所有 TypeScript 文件,可以使用以下前辅文块:

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

    Glob 示例:

    • * - 会匹配当前目录中的所有文件。
    • ****/* - 均会匹配所有目录中的所有文件。
    • *.py - 将匹配当前目录中的所有 .py 文件。
    • **/*.py - 将以递归方式匹配所有目录中的所有 .py 文件。
    • src/*.py - 将匹配 .py 目录中所有 src 文件。 例如,src/foo.pysrc/bar.py但_不_src/foo/bar.py
    • src/**/*.py - 将以递归方式匹配目录中的所有 .py 文件 src 。 例如 、 src/foo.py``src/foo/bar.pysrc/foo/bar/baz.py
    •           `**/subdir/**/*.py` - 将递归匹配任意深度下任意 `subdir` 目录中的所有 `.py` 文件。 例如,`subdir/foo.py`、`subdir/nested/bar.py`、`parent/subdir/baz.py` 和 `deep/parent/subdir/nested/qux.py`,但不匹配_不_`foo.py`包含 `subdir` 目录的路径。
      
  5. 或者,为了防止文件被 Copilot云代理 或 Copilot 代码评审 使用,将 excludeAgent 关键字添加到 frontmatter 区块中。 使用 "code-review""cloud-agent"

    例如,以下文件将仅由 Copilot云代理 读取。

    ---
    applyTo: "**"
    excludeAgent: "code-review"
    ---
    

    如果在 front matterblock 中不包括关键字 excludeAgent,则 Copilot 代码评审 和 Copilot云代理 都将使用您的说明。

  6. 使用 Markdown 格式以自然语言添加自定义指令。 系统会忽略说明信息间的空格,因此可将信息编写为一个段落,每个段落位于一行上,或用空白行分隔,以保持其可读性。

你是否已成功将自定义指令文件添加到你的仓库中?

          <a href="https://docs.github.io/success-test/yes.html" target="_blank" class="btn btn-outline mt-3 mr-3 no-underline">
          <span>是</span></a><a href="https://docs.github.io/success-test/no.html" target="_blank" class="btn btn-outline mt-3 mr-3 no-underline"><span>否</span></a>

正在使用的自定义说明

文件(s)中的说明可在保存文件后立即使用 Copilot 。 指令会自动添加到您提交给Copilot的请求中。

In 副驾驶聊天 (github.com/copilot), you can start a conversation that uses repository custom instructions by adding, as an attachment, the repository that contains the instructions file.

Whenever repository custom instructions are used by 副驾驶聊天, the instructions file is added as a reference for the response that's generated. To find out whether repository custom instructions were used, expand the list of references at the top of a chat response in the Chat panel and check whether the .github/copilot-instructions.md file is listed.

Screenshot of an expanded References list, showing the 'copilot-instructions.md' file highlighted with a dark orange outline.

You can click the reference to open the file.

注意

  • 多种类型的自定义指令可以应用于发送到 Copilot的请求。 个人指令采用最高优先级。 接下来是存储库说明,然后组织说明将优先安排在最后。 但是,向 Copilot提供了所有相关指令集。
  • 尽可能避免提供冲突的指令集。 如果担心响应质量,可以暂时禁用存储库说明。 请参阅“为GitHub Copilot添加存储库自定义说明”。

Enabling or disabling custom instructions for Copilot 代码评审

Custom instructions are enabled for Copilot 代码评审 by default but you can disable, or re-enable, them in the repository settings on GitHub.com. This applies to Copilot's use of custom instructions for all code reviews it performs in this repository.

  1. 在 GitHub 上,导航到存储库的主页面。

  2. 在仓库名称下,单击 “Settings”****。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。

    存储库标头的屏幕截图,其中显示了选项卡。 “设置”选项卡以深橙色边框突出显示。

  3. In the "Code & automation" section of the sidebar, click Copilot, then Code review.

  4. Toggle the “Use custom instructions when reviewing pull requests” option on or off.

注意

评审拉取请求时,Copilot 使用拉取请求基础分支中的自定义指令。 例如,如果你的拉取请求旨在将 my-feature-branch 合并到 main,Copilot 将使用 main 中的自定义指令。

Further reading