Skip to main content

为 GitHub Copilot 配置内容排除

你可以阻止指定的文件被用于 GitHub Copilot 提供的代码补全建议。 GitHub Copilot 在被排除的文件中不可用。

谁可以使用此功能?

Repository administrators and organization owners can manage the content exclusion settings for GitHub Copilot.

People with the "Maintain" role for a repository can view the content exclusion settings for that repository, but can't change these settings. For more information, see "组织的存储库角色."

This feature is available for organization accounts with a Copilot Business subscription.

注意:

  • 从 GitHub Copilot 中排除的内容目前为公开 Beta 版,可能会有变动。
  • Visual Studio、Visual Studio Code、JetBrains IDE 和 neovim 中支持此功能。 有关受支持的 JetBrains IDE 的详细信息,请参阅“开始使用 GitHub Copilot”。

关于配置内容排除

你可能希望阻止某些文件用于 GitHub Copilot。 你可以配置 GitHub Copilot 以便它忽略这些文件。 可以通过在存储库或组织的设置中指定排除内容的路径来执行此操作。

指定内容排除时,会产生两个影响:

  • GitHub Copilot 不会使用受影响文件的内容来为其他文件中的代码补全建议提供信息。
  • GitHub Copilot 代码补全在受影响的文件中不可用。

添加或更改内容排除项后,可能需要长达 30 分钟才能在已加载设置的 IDE 中生效。 你可以对自己的 IDE 应用更改,强制它重新加载内容排除项的设置。 有关更多信息,请参阅“将内容排除更改传播到 IDE”。

限制

  • 从 GitHub Copilot 中排除的内容目前仅影响代码完成。 GitHub Copilot Chat 不会受这些设置的影响。
  • 内容排除项可防止 Copilot 直接访问已排除文件的内容。 Copilot 可能会从 IDE 提供的语义信息(例如,类型信息或代码中所使用符号的悬停定义)中绘制有关非排除文件的信息。 IDE 可能会从排除的文件派生此信息。

可以排除哪些内容?

在存储库的设置中指定内容排除项时,只能排除该存储库中的文件。

在组织的设置中指定内容排除项时,可以排除在 GitHub.com 上托管的任何基于 Git 的存储库中的文件,或者排除使用以下任一语法访问的文件:

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/

注意: 请务必注意,可以使用订阅了 Copilot Business 的任何 GitHub 组织的设置排除内容。 排除的文件可能位于不属于定义排除的组织的存储库中。 排除的文件也可能位于不托管在 GitHub 上的基于 Git 的存储库中。

哪些用户受内容排除项设置的影响?

GitHub Copilot 内容排除项设置不适用于使用 Copilot 的每个人。

内容排除项设置仅适用于以下人员:作为 GitHub Copilot Business 或 GitHub Copilot Enterprise 订阅成员已授予席位,并且是已配置内容排除项的同一组织的成员。 可访问指定文件的其他用户不会受到内容排除项的影响,并且仍会看到代码完成建议。

所有排除项,无论是在存储库设置中还是在组织设置中定义,都适用于已作为 Copilot Business 订阅一部分被授予 Copilot 席位的组织的所有成员。

将数据发送到 GitHub

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

为存储库配置内容排除

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

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

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

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

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

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

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

    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 secret anywhere in this repository.

- "*.cfg"

Ignore files whose names end .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 `secret` anywhere in this repository.
- "secret*"

# Ignore files whose names end `.cfg` anywhere in this repository.
- "*.cfg"

# Ignore all files in or below the `/scripts` directory of this repository.
- "/scripts/**"

配置组织的内容排除

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

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

    @octocat 的个人资料图片下的下拉菜单的屏幕截图。 “你的组织”以深橙色标出。

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

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

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

    对于要从 Copilot 中排除文件的每个存储库,请在一行中输入对存储库的引用,后跟存储库中位置的路径,每条路径单独一行。 使用以下格式:

    REPOSITORY-REFERENCE:
      - "/PATH/TO/DIRECTORY/OR/FILE"
      - "/PATH/TO/DIRECTORY/OR/FILE"
      - ...
    

    REPOSITORY-REFERENCE 支持以下语法:

    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 部分。
    • 每个存储库引用可以包含一个 * 通配符。 例如,https://github.com/octo-org/* 匹配 octo-org 组织中的所有存储库。

    可以使用 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 server or session, anywhere in this repository.

  - "*.m[dk]"

Ignore any files with names ending .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 `server` or `session`, anywhere in this repository.
  - "{server,session}*"
  # Ignore any files with names ending `.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/**"

查看对内容排除设置的更改

如果你是组织所有者,则可以查看对内容排除所做的任何更改。

  1. 打开组织设置中的“内容排除”页面(此处所述)或存储库的设置(此处所述)。

  2. 滚动到页面底部。

    你将看到上次更改设置人员的姓名,及其何时进行此更改的信息。

  3. 单击上次更改的时间。

    上次编辑的信息的屏幕截图。 更改时间链接以深橙色边框突出显示。

    注意****:如果你是组织所有者,则上次更改的时间只是一个链接。

    将显示组织的“审核日志”页,其中显示了最近记录的 copilot.content_exclusion_changed 操作次数。

    如果从存储库设置页中单击,则会筛选审核日志,仅显示对该存储库内容排除的更改。

  4. 单击每个条目末尾的省略号 (...),查看更多详细信息。

    如果截断了 "excluded_paths" 条目,请将鼠标悬停在截断值上以显示完整条目。 此操作会显示保存更改后排除设置的内容。

    'copilot.content_exclusion_changed' 操作的审核日志详细信息的屏幕截图。 省略号按钮已突出显示。

检查设置更改的效果

更改 Copilot 的内容排除时,可以检查设置阻止 Copilot 在指定文件中建议代码。

要确认已为此文件禁用 Copilot,可以在编辑器中打开该文件,并开始键入一行代码,比如注释。 通常,键入时会看到 Copilot 的代码补全建议。 如果 Copilot 被内容排除项禁用,则不会提供代码完成建议,并且该文件的内容不会用于在其他文件中生成建议。

检查 IDE 中的设置更改

如果使用 Visual Studio、VS Code 或受支持的 JetBrains IDE,Copilot 图标指示何时通过内容排除项禁用 Copilot。

  1. 打开预期会受内容排除项影响的文件。

    如果将 Copilot 内容排除应用到此文件,则状态栏中的 Copilot 图标将有一条对角线穿过。

  2. 将鼠标悬停在此图标上。 弹出消息告知组织或父存储库是否为此文件禁用了 Copilot。

    VS Code 工具栏中已禁用 Copilot 弹出消息的屏幕截图。

**** 注意:在 Visual Studio 和 VS Code 中,可以显示 Copilot 扩展的日志,以查看内容排除项的详细信息,而无需将鼠标悬停在 Copilot 图标上。 有关详细信息,请参阅“查看环境中的 GitHub Copilot 日志”。

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

如果使用的是 Neovim,则每次打开文件时,都会从 GitHub 提取内容排除项。 但是,如果使用 Visual Studio、VS Code 或受支持的 JetBrains IDE,可能需要等待最多 30 分钟才能看到设置更改的效果。 或者,可以手动将内容排除项设置重新加载到 IDE 中。

若要在 Visual Studio 中重新加载内容排除项,请关闭并重新打开应用程序。

要在 VS Code 中重新加载内容排除项:

  1. 访问命令面板。 例如,通过按 Shift+Command+P (Mac)/Ctrl+Shift+P (Windows/Linux)。
  2. 键入:reload
  3. 选择“开发人员:重新加载窗口”****。

若要将内容排除项重新加载到 JetBrains IDE 中,请关闭并重新打开应用程序,或退出 GitHub 然后重新登录,如下所示。

  1. 单击状态栏中的 Copilot 图标,然后选择“从 GitHub 退出”****。
  2. 状态栏中的 Copilot 图标现在将有一条对角线。 单击它并选择“登录到 GitHub”****。
  3. 显示“登录到 GitHub”消息,显示设备代码。 单击“复制并打开”****。
  4. 在“设备激活”页上,粘贴设备代码,然后单击“继续”****。
  5. 在下一页上,单击****“授权 GitHub Copilot 插件”。

延伸阅读