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业务 或 Copilot Enterprise 计划的组织。

为存储库配置内容排除

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

注意

          GitHub Copilot 命令行界面 (CLI)、 Copilot云代理和 IDE 中的 副驾驶聊天 代理模式不支持内容排除。 有关这些Copilot功能的详细信息,请参阅[AUTOTITLE](/copilot/concepts/agents/about-copilot-cli)、[AUTOTITLE](/copilot/concepts/agents/cloud-agent/about-cloud-agent) 和[AUTOTITLE](/copilot/how-tos/chat-with-copilot/chat-in-ide)。
  1. 在 GitHub 上,导航到存储库的主页面。

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

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

  3. 在边栏的“代码和自动化”部分中,单击 Copilot,然后单击 内容排除

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

  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 的右上角,单击个人资料图片,然后单击“ Your organizations”****。

  2. 单击以选择一个组织。

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

    组织配置文件中选项卡的屏幕截图。 “设置”选项卡以深橙色标出。

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

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

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

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

    在计算要忽略存储库的路径时,将忽略 user@:portREPOSITORY-REFERENCE 部分。

    对于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. 导航到您的企业。 例如,从 GitHub.com 上的 公司 页面。
  2. 在页面顶部,单击 AI 控件
  3. 在边栏中,单击 Copilot**。
  4. 单击“ 内容排除”。
  5. 使用路径指定要排除的内容。 请参阅上一部分:为组织配置内容排除

注意

在企业级别设置内容排除与组织级别的主要区别在于,在企业级别设置的规则适用于 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. 要测试 副驾驶聊天 的内容排除:
    • 在你的 IDE 中打开 副驾驶聊天 面板。

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

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

    • 输入提示 explain this file

      如果排除了该文件, 副驾驶聊天 则无法使用该文件生成响应。 该文件不会作为 Copilot 响应中的引用列出。

使用 REST API 管理内容排除项

组织所有者和企业所有者还可以使用 GitHub REST API 以编程方式管理内容排除。

有关详细信息,请参阅“用于Copilot内容排除管理的 REST API 端点”。

其他阅读材料