Skip to main content

从 GitHub Copilot 中排除内容

可以阻止 Copilot 访问某些内容。

谁可以使用此功能?

存储库管理员和组织所有者可以管理内容排除设置。

具有存储库“维护”角色的人员可以查看该存储库的内容排除设置,但不能对其进行编辑。

订阅 GitHub Copilot Business 或 GitHub Copilot Enterprise 的组织

关于 Copilot 的内容排除项

Note

从 GitHub Copilot 中排除的内容目前为公开 Beta 版,可能会有变动。

可以使用内容排除项来配置 Copilot,以忽略存储库或组织中的某些文件。 从 Copilot 中排除内容时:

  • 代码完成功能在受影响的文件中不可用。
  • 受影响文件中的内容不会用于其他文件中的代码完成建议。
  • 受影响文件中的内容不会用于 GitHub Copilot Chat 的回答。

内容排除项设置仅适用于配置了内容排除项的组织的成员,这些成员已作为 GitHub Copilot Business 或 GitHub Copilot Enterprise 订阅的一部分被授予席位。

任何其他可以访问指定文件的人仍将看到引用指定文件的代码完成建议和 GitHub Copilot Chat 响应。

内容排除功能的限制

内容排除项当前不适用于 Visual Studio 中的 Copilot 聊天、GitHub.com 中的 Copilot 聊天 或 Azure Data Studio 中的代码完成。 此外:

  • 在 Visual Studio Code 中的 Copilot Chat 中,当在问题中使用 @github 聊天参与者时,不会应用内容排除项。
  • 如果 IDE 在未排除的文件中提供了 Copilot 中的语义信息,则可能会使用该信息。 此类内容的示例包括代码中使用的符号的类型信息和悬停定义。

将数据发送到 GitHub

配置内容排除项后,客户端(例如,VS Code 的 Copilot 扩展)将当前存储库 URL 发送到 GitHub 服务器,以便服务器可以将正确的策略返回至客户端。 这些 URL 不会记录到任何位置。

为存储库配置内容排除

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

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

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

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

  3. 在边栏的“代码和自动化”部分中,单击 Copilot

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

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

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

    Tip

    可以使用 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 存储库中的内容。

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

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

  3. 在左侧边栏中,单击 Copilot,然后单击“内容排除”****。

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

    对于要从 Copilot 中排除文件的每个存储库,请在一行中输入对存储库的引用,后跟存储库中位置的路径,每条路径单独一行。 使用以下格式,将 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 都会将其匹配。

    Tip

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

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

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

Ignore all .env files at any path, in any repository. This setting applies to all repositories, not just to those on GitHub.com. 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 at any path, in any repository.
# This setting applies to all repositories, not just to those on GitHub.com.
# 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/**"

测试对内容排除项的更改

可以使用 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 提取内容排除项。

测试内容排除项

可通过几种不同的方法来测试内容排除项,具体取决于所使用的 IDE。

  1. 打开预期会受内容排除项影响的文件。
  2. 使用以下一种或多种技术来测试内容是否被排除:
    • 在 JetBrains IDE、Visual Studio 和 Visual Studio Code 中,检查状态栏中的 Copilot 图标。 如果将 Copilot 内容排除应用到该文件,则 Copilot 图标将有一条对角线穿过。 将鼠标悬停在图标上,查看组织或父存储库是否禁用了文件的 Copilot。
    • 在 JetBrains IDE 和 Visual Studio Code 中,还可以测试 Copilot Chat 中的内容排除项。 打开 Copilot Chat 窗口,并向 Copilot Chat 询问有关已排除文件的问题。 如果内容已成功排除,则 Copilot 将无法回答你的问题,并解释由于内容排除规则,某些文件已从对话中排除。
    • 在 Vim/Neovim 中,开始键入文件。 如果 GitHub Copilot 在键入时不再提供内联建议,则表明该文件已被排除。

延伸阅读