Skip to main content

从 GitHub Copilot 中排除内容

了解如何阻止 Copilot 访问某些内容。

谁可以使用此功能?

Repository administrators, organization owners, and enterprise owners can manage content exclusion settings. People with the "Maintain" role for a repository can view, but not edit, content exclusion settings for that repository.

拥有 Copilot Business 或 Copilot Enterprise 计划的组织。

为存储库配置内容排除

可以使用存储库设置指定 GitHub Copilot 应忽略的存储库中的内容。

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

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

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

  3. 在边栏的“Code & automation”部分,单击 Copilot,然后单击“Content exclusion”********。

    如果存储库从其父组织或同一企业中的组织继承了任何排除项,你将在页面顶部看到一个或多个灰色框,其中包含这些排除项的详细信息。 你无法编辑这些设置。

  4. 在“要在此存储库中排除的路径”之后的框中,输入应从中排除 Copilot 的文件的路径。

    使用格式:- "/PATH/TO/DIRECTORY/OR/FILE",每条路径单独一行。 可以通过以 # 开头的行来添加注释。

    提示

    可以使用 fnmatch 模式匹配表示法来指定文件路径。 模式不区分大小写。 请参阅 ruby-doc.org 文档中的“文件”。

存储库设置中指定的路径示例

YAML
- "/src/some-dir/kernel.rs"

Ignore the /src/some-dir/kernel.rs file in this repository.

- "secrets.json"

Ignore files called secrets.json anywhere in this repository.

- "secret*"

Ignore all files whose names begin with secret anywhere in this repository.

- "*.cfg"

Ignore files whose names end with .cfg anywhere in this repository.

- "/scripts/**"

Ignore all files in or below the /scripts directory of this repository.

# 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 控制的文件系统上的任意位置。

  1. 在 GitHub 的右上角,选择个人资料照片,然后单击“ 你的组织”****。

  2. 在组织旁边,单击“设置”。

  3. 在左侧边栏中,单击“ Copilot”,然后单击“Content exclusion”********。

  4. 在“要排除的存储库和路径”之后的框中,输入应从中排除 Copilot 的文件的详细信息。

    若要排除位于任意位置(Git 存储库或其他地方)的文件,请输入 "*":,后跟文件的路径,或要排除的文件。 如果要指定多个文件路径模式,请在单独的行上列出每个模式。

    要从 Copilot 中排除 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-REFERENCEuser@:port 部分。

    对于 Azure DevOps,可以在指定 REPOSITORY-REFERENCE 时使用新版 (dev.azure.com) 或旧版 (visualstudio.com) 主机格式,无论使用哪台主机在本地克隆存储库,Copilot 都会将其匹配。

    提示

    可以使用 fnmatch 模式匹配表示法来指定文件路径。 模式不区分大小写。 请参阅 ruby-doc.org 文档中的“文件”。

组织设置中的存储库和路径示例

YAML
"*":
  - "**/.env"

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"]

octo-repo:

In the octo-repo repository in this organization:

  - "/src/some-dir/kernel.rs"

Ignore the /src/some-dir/kernel.rs file.

https://github.com/primer/react.git:

In the primer/react repository on GitHub:

  - "secrets.json"

Ignore files called secrets.json anywhere in this repository.

  - "/src/**/temp.rb"

Ignore files called temp.rb in or below the /src directory.

git@github.com:*/copilot:

In the copilot repository of any GitHub organization:

  - "/__tests__/**"

Ignore any files in or below the /__tests__ directory.

  - "/scripts/*"

Ignore any files in the /scripts directory.

git@gitlab.com:gitlab-org/gitlab-runner.git:

In the gitlab-org/gitlab-runner repository on GitLab:

  - "/main_test.go"

Ignore the /main_test.go file.

  - "{server,session}*"

Ignore any files with names beginning with server or session anywhere in this repository.

  - "*.m[dk]"

Ignore any files with names ending with .md or .mk anywhere in this repository.

  - "**/package?/*"

Ignore files directly within directories such as packages or packaged anywhere in this repository.

  - "**/security/**"

Ignore files in or below any security directories, anywhere in this repository.

# 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 控制的文件系统上的任意位置。

  1. 在页面顶部,单击“ Policies”****。
  2. 在“Policies”部分中,单击“Copilot”****。
  3. 单击“内容排除”选项卡。
  4. 使用路径指定要排除的内容。 请参阅上一部分为组织配置内容排除

注意

在企业级别和组织级别设置内容排除之间的主要区别在于,在企业级别设置的规则适用于企业中的所有 Copilot 用户,而组织所有者设置的规则仅适用于由该组织分配了 Copilot 席位的用户。

测试对内容排除项的更改

可以使用 IDE 确认对内容排除项的更改是否按预期工作。

将内容排除项更改传播到 IDE

添加或更改内容排除项后,可能需要长达 30 分钟才能在已加载设置的 IDE 中生效。 如果不想等待,则可以使用以下说明手动重新加载内容排除项设置。

  • 对于 JetBrains IDE 和 Visual Studio,请关闭并重新打开应用程序来重新加载内容排除项设置。
  • 对于 Visual Studio Code,请使用以下步骤来重新加载内容排除设置:
    1. 访问命令面板。 例如,通过按 Shift+Command+P (Mac)/Ctrl+Shift+P (Windows/Linux)。
    2. 键入:reload
    3. 选择“开发人员:重新加载窗口”****。
  • 对于 Vim/Neovim,每次打开文件时,都会自动从 GitHub 提取内容排除项。

测试内容排除项

可以验证已配置的内容排除是否按预期工作。

  1. 首先确认内容排除在不受内容排除约束的文件中可以正常工作。 要执行此操作:
    • 在 IDE 中,打开不受内容排除约束的文件。
    • 进行编辑,这通常会生成代码补全建议。 系统应提供建议
  2. 打开应排除的文件并进行相同的编辑。 系统不应提供任何建议。
  3. 若要测试 Copilot Chat 的内容排除,请执行以下步骤:
    • 在 IDE 中,打开 Copilot Chat 面板。

    • 打开应排除的文件并关闭编辑器中打开的任何其他文件。

    • 确保打开的文件附加到提示作为上下文文件。

    • 输入提示 explain this file

      如果排除了该文件,Copilot Chat 将无法使用该文件生成答复。 该文件不会在 Copilot 的答复中列为参考。

其他阅读材料