为您的仓库配置内容排除
您可以使用仓库设置来指定 GitHub Copilot 应该忽略的仓库内容。
注意
GitHub Copilot CLI、Copilot 云代理以及 IDE 中的 Copilot Chat 代理模式不支持内容排除。有关这些 Copilot 功能的更多信息,请参阅关于 GitHub Copilot CLI、关于 GitHub Copilot 云代理以及在 IDE 中向 GitHub Copilot 提问。
-
在 GitHub 上,导航至仓库的主页面。
-
在仓库名称下,点击 设置。如果看不到 “设置” 选项卡,请选择 下拉菜单,然后点击 设置。

-
在侧边栏的 “代码 & 自动化” 部分,点击 Copilot,然后点击 内容排除。
如果您的仓库从其父组织或同一企业中的其他组织继承了任何排除设置,页面顶部会出现一个或多个灰色框,显示这些排除的详情。您无法编辑这些设置。
-
在 “此仓库中要排除的路径” 框中,输入 Copilot 应该排除的文件路径。
使用以下格式:
- "/PATH/TO/DIRECTORY/OR/FILE",每行一个路径。您可以在行首加#来添加注释。提示
您可以使用 fnmatch 模式匹配符号来指定文件路径。模式不区分大小写。请参阅 ruby‑doc.org 文档中的 File。
在仓库设置中指定的路径示例
# Ignore the `/src/some-dir/kernel.rs` file in this repository. - "/src/some-dir/kernel.rs" # Ignore files called `secrets.json` anywhere in this repository. - "secrets.json" # Ignore all files whose names begin with `secret` anywhere in this repository. - "secret*" # Ignore files whose names end with `.cfg` anywhere in this repository. - "*.cfg" # Ignore all files in or below the `/scripts` directory of this repository. - "/scripts/**"
- "/src/some-dir/kernel.rs"在此仓库中忽略 /src/some-dir/kernel.rs 文件。
- "secrets.json"在此仓库中忽略任何名为 secrets.json 的文件。
- "secret*"在此仓库中忽略所有文件名以 secret 开头的文件。
- "*.cfg"在此仓库中忽略所有文件名以 .cfg 结尾的文件。
- "/scripts/**"在此仓库中忽略 /scripts 目录及其子目录中的所有文件。
# Ignore the `/src/some-dir/kernel.rs` file in this repository.
- "/src/some-dir/kernel.rs"
# Ignore files called `secrets.json` anywhere in this repository.
- "secrets.json"
# Ignore all files whose names begin with `secret` anywhere in this repository.
- "secret*"
# Ignore files whose names end with `.cfg` anywhere in this repository.
- "*.cfg"
# Ignore all files in or below the `/scripts` directory of this repository.
- "/scripts/**"
为您的组织配置内容排除
您可以使用组织设置来指定 GitHub Copilot 应该忽略的文件。文件可以位于 Git 仓库内,也可以位于未受 Git 管理的文件系统任意位置。
-
在 GitHub 右上角,点击您的头像,然后点击 组织。
-
点击即可选择组织。
-
在您的组织名称下,点击 设置。如果看不到 “设置” 选项卡,请选择 下拉菜单,然后点击 设置。

-
在左侧边栏,点击 Copilot,然后点击 内容排除。
-
在 “要排除的仓库和路径” 框中,输入要从 Copilot 排除的文件的详细信息。
要排除位于任意位置(Git 仓库内或其他位置)的文件,请输入
"*",后跟要排除的文件或文件路径。如果要指定多个文件路径模式,请将每个模式单独写在一行。要在 Git 仓库中排除文件,请在一行输入对该仓库的引用,然后在后面的行中输入该仓库内的路径,每行一个。使用以下格式,将
REPOSITORY-REFERENCE替换为包含您想排除文件的仓库引用。REPOSITORY-REFERENCE: - "/PATH/TO/DIRECTORY/OR/FILE" - "/PATH/TO/DIRECTORY/OR/FILE" - ...仓库可以使用多种协议进行引用。您可以使用以下任意一种语法作为
REPOSITORY-REFERENCE,Copilot 会在本地克隆方式不同的情况下仍能匹配。http[s]://host.xz[:port]/path/to/repo.git/ git://host.xz[:port]/path/to/repo.git/ [user@]host.xz:path/to/repo.git/ ssh://[user@]host.xz[:port]/path/to/repo.git/REPOSITORY-REFERENCE中的user@和:port部分在计算要忽略的路径时会被忽略。对于 Azure DevOps,您可以在指定
REPOSITORY-REFERENCE时使用新(dev.azure.com)或旧(visualstudio.com)主机格式,Copilot 会在本地克隆时匹配相应的仓库,无论使用哪个主机。提示
您可以使用 fnmatch 模式匹配符号来指定文件路径。模式不区分大小写。请参阅 ruby‑doc.org 文档中的 File。
组织设置中仓库和路径的示例
# Ignore all `.env` files from all file system roots (Git and non-Git).
# For example, this excludes `REPOSITORY-PATH/.env` and also `/.env`.
# This could also have been written on a single line as:
#
# "*": ["**/.env"]
"*":
- "**/.env"
# In the `octo-repo` repository in this organization:
octo-repo:
# Ignore the `/src/some-dir/kernel.rs` file.
- "/src/some-dir/kernel.rs"
# In the `primer/react` repository on GitHub:
https://github.com/primer/react.git:
# Ignore files called `secrets.json` anywhere in this repository.
- "secrets.json"
# Ignore files called `temp.rb` in or below the `/src` directory.
- "/src/**/temp.rb"
# In the `copilot` repository of any GitHub organization:
git@github.com:*/copilot:
# Ignore any files in or below the `/__tests__` directory.
- "/__tests__/**"
# Ignore any files in the `/scripts` directory.
- "/scripts/*"
# In the `gitlab-org/gitlab-runner` repository on GitLab:
git@gitlab.com:gitlab-org/gitlab-runner.git:
# Ignore the `/main_test.go` file.
- "/main_test.go"
# Ignore any files with names beginning with `server` or `session` anywhere in this repository.
- "{server,session}*"
# Ignore any files with names ending with `.md` or `.mk` anywhere in this repository.
- "*.m[dk]"
# Ignore files directly within directories such as `packages` or `packaged` anywhere in this repository.
- "**/package?/*"
# Ignore files in or below any `security` directories, anywhere in this repository.
- "**/security/**"
"*":
- "**/.env"忽略所有根目录(包括 Git 和非 Git)下的 .env 文件。例如,这会排除 REPOSITORY-PATH/.env 以及 /.env。也可以写成单行:
"*": ["**/.env"]
octo-repo:在本组织的 octo-repo 仓库中
- "/src/some-dir/kernel.rs"忽略 /src/some-dir/kernel.rs 文件。
https://github.com/primer/react.git:在 GitHub 上的 primer/react 仓库中
- "secrets.json"在此仓库中忽略任何名为 secrets.json 的文件。
- "/src/**/temp.rb"忽略 /src 目录及其子目录中名为 temp.rb 的文件。
git@github.com:*/copilot:在任意 GitHub 组织的 copilot 仓库中
- "/__tests__/**"忽略 /__tests__ 目录及其子目录下的所有文件。
- "/scripts/*"忽略 /scripts 目录中的所有文件。
git@gitlab.com:gitlab-org/gitlab-runner.git:在 GitLab 上的 gitlab-org/gitlab-runner 仓库中
- "/main_test.go"忽略 /main_test.go 文件。
- "{server,session}*"忽略此仓库中任何文件名以 server 或 session 开头的文件。
- "*.m[dk]"忽略此仓库中任何文件名以 .md 或 .mk 结尾的文件。
- "**/package?/*"忽略此仓库中直接位于 packages 或 packaged 等目录下的文件。
- "**/security/**"忽略此仓库中任何 security 目录及其子目录下的文件。
# Ignore all `.env` files from all file system roots (Git and non-Git).
# For example, this excludes `REPOSITORY-PATH/.env` and also `/.env`.
# This could also have been written on a single line as:
#
# "*": ["**/.env"]
"*":
- "**/.env"
# In the `octo-repo` repository in this organization:
octo-repo:
# Ignore the `/src/some-dir/kernel.rs` file.
- "/src/some-dir/kernel.rs"
# In the `primer/react` repository on GitHub:
https://github.com/primer/react.git:
# Ignore files called `secrets.json` anywhere in this repository.
- "secrets.json"
# Ignore files called `temp.rb` in or below the `/src` directory.
- "/src/**/temp.rb"
# In the `copilot` repository of any GitHub organization:
git@github.com:*/copilot:
# Ignore any files in or below the `/__tests__` directory.
- "/__tests__/**"
# Ignore any files in the `/scripts` directory.
- "/scripts/*"
# In the `gitlab-org/gitlab-runner` repository on GitLab:
git@gitlab.com:gitlab-org/gitlab-runner.git:
# Ignore the `/main_test.go` file.
- "/main_test.go"
# Ignore any files with names beginning with `server` or `session` anywhere in this repository.
- "{server,session}*"
# Ignore any files with names ending with `.md` or `.mk` anywhere in this repository.
- "*.m[dk]"
# Ignore files directly within directories such as `packages` or `packaged` anywhere in this repository.
- "**/package?/*"
# Ignore files in or below any `security` directories, anywhere in this repository.
- "**/security/**"
为您的企业配置内容排除
作为企业所有者,您可以使用企业设置来指定 GitHub Copilot 应该忽略的文件。文件可以位于 Git 仓库内,也可以位于未受 Git 管理的文件系统任意位置。
- 转到您的企业。例如,在 GitHub.com 上的 企业 页面。
- 在页面顶部,点击 AI 控制。
- 在侧边栏,点击 Copilot.
- 点击 内容排除.
- 使用路径来指定要排除的内容。请参阅前面的章节,为您的组织配置内容排除。
注意
在企业级别设置内容排除与在组织级别设置的关键区别在于,企业级别的规则适用于企业内的所有 Copilot 用户,而组织所有者设置的规则仅适用于该组织为其分配了 Copilot 帐号的用户。
测试内容排除的更改
您可以使用 IDE 来确认内容排除的更改是否按预期工作。
将内容排除的更改传播到您的 IDE
添加或更改内容排除后,已加载设置的 IDE 中可能需要最多 30 分钟才能生效。如果不想等待,可以按照以下说明手动重新加载内容排除设置。
- 对于 JetBrains IDE 和 Visual Studio,通过关闭并重新打开应用程序来重新加载内容排除设置。
- 对于 Visual Studio Code,使用以下步骤重新加载内容排除设置:
- 打开命令面板。例如,按 Shift+Command+P(Mac)或 Ctrl+Shift+P(Windows/Linux)。
- 输入:
reload。 - 选择 Developer: Reload Window。
- 对于 Vim/Neovim,每次打开文件时内容排除会自动从 GitHub 获取。
测试您的内容排除
您可以验证已配置的内容排除是否按预期工作。
- 首先确认在未受内容排除影响的文件中内容排除是否正常工作。操作如下
- 在您的 IDE 中,打开一个未受内容排除影响的文件。
- 进行一次通常会生成内联建议的编辑。应出现建议。
- 打开一个应该被排除的文件并进行相同的编辑。不会出现建议。
- 测试 Copilot Chat 的内容排除
-
在您的 IDE 中,打开 Copilot Chat 面板。
-
打开应被排除的文件并关闭编辑器中打开的其他文件。
-
确保打开的文件已作为上下文文件附加到提示中。
-
输入提示
explain this file。如果文件被排除,Copilot Chat 将无法使用该文件生成响应。该文件也不会出现在 Copilot 响应的引用列表中。
-
使用 REST API 管理内容排除
组织所有者和企业所有者也可以使用 GitHub REST API 以编程方式管理内容排除。
欲了解更多信息,请参阅 Copilot 内容排除管理的 REST API 端点。