Skip to main content

查找与 GitHub Copilot 建议匹配的公开代码

如果允许 GitHub Copilot 提供与公开可用的代码匹配的建议,Copilot 会显示对找到的任何匹配代码的引用。

This version of this article is for Copilot on the GitHub website. For the Visual Studio Code version of this article, click the tab above.

This version of this article is for Copilot in Visual Studio Code. For the GitHub website version of this article, click the tab above.

Limitations

References to matching code are currently only available in Visual Studio Code and on the GitHub website.

Prerequisites

References to matching code are only generated if Copilot is configured to allow suggestions that match publicly available code. For more information, see "Managing Copilot policies as an individual subscriber."

About code referencing in GitHub Copilot

If you've allowed suggestions that match public code, GitHub Copilot in VS Code can provide you with details of the code that a suggestion matches. This happens:

  • When you accept a code completion suggestion in the editor.
  • When a response in Copilot Chat includes matching code.

If you've allowed suggestions that match public code, then whenever a response from Copilot Chat includes matching code, details of the matches will be included in the response.

This feature is called code referencing.

Note

Typically, matches to public code occur infrequently, so you should not expect to see code references in many Copilot Chat responses.

Code referencing for Copilot code completion

When you accept a code completion suggestion that matches code in a public GitHub repository, an entry is added to a GitHub Copilot log. The log entry includes a link to a page on GitHub.com where you can view references to similar code in public GitHub repositories.

The linked web page includes details of any license identified for the repository where the matching code was found. Having reviewed the references, you can decide how to proceed. For example, you can decide what attribution to use, or whether you want to remove this code from your project.

Note

  • Code referencing for code completion only occurs for matches of accepted Copilot suggestions. Code you have written, and Copilot suggestions you have altered, are not checked for matches to public code.
  • Typically, matches to public code occur in less than one percent of Copilot suggestions, so you should not expect to see code references for many suggestions.

Code referencing for Copilot Chat

When Copilot Chat provides a response that includes code that matches code in a public GitHub repository, this is indicated at the end of the response with a link to display details of the matched code in the VS Code editor.

How code referencing finds matching code

Copilot code referencing searches for matches by taking the code suggestion, plus some of the code that will surround the suggestion if it is accepted, and comparing it against an index of all public repositories on GitHub.com. Code in private GitHub repositories, or code outside of GitHub, is not included in the search process. The search index is refreshed every few months. As a result, newly committed code, and code from public repositories deleted before the index was created, may not be included in the search. For the same reason, the search may return matches to code that has been deleted or moved since the index was created.

View code references for code completion

You can access code references from one of the GitHub Copilot logs in Visual Studio Code.

  1. In Visual Studio Code, open the Output window by selecting View > Output from the menu bar.

  2. In the dropdown menu at the right of the Output window, select GitHub Copilot Log.

    Screenshot of the Output window with "GitHub Copilot Log" selected from the dropdown menu.

  3. Leave the GitHub Copilot Log view displayed while you use GitHub Copilot in Visual Studio Code.

    When you accept a code completion suggestion that matches code in a public GitHub repository, an entry is added to the log.

    The log entry includes the following details:

    • The date and time you accepted the suggestion.
    • The name of the file in which the suggestion was added.
    • A message telling you that similar code was found, with the number and type of licenses.
    • A link to a page on GitHub.com.
    • The location in the file where the suggestion was added.
    • A snippet of part of the code that was suggested and added.

    Screenshot of the Output window showing a code referencing log entry.

    Note

    The GitHub Copilot log is flushed when you close the editor.

  4. In the log entry, Ctrl+click (Windows/Linux) or Command+click (Mac) the link to view the code references on GitHub.com.

    The page lists the matches that were found, showing an excerpt from the relevant file. You can click on the file name to view the whole file.

    Screenshot of code references listed on GitHub.com.

    Matches may be found in multiple repositories with different, sometimes conflicting, licenses. You can use the checkboxes on the left of the page to show only the matches associated with a particular license.

    You should review all license information within a repository to verify that it applies to the matching code, as repositories may contain multiple licenses and these licenses are subject to human and non-human error.

    Note

    The web page displaying the full set of details will remain available for three months.

Verifying the code referencing functionality

You can verify that code referencing is working by prompting Copilot to add some commonly used code and checking the output in the Copilot log.

  1. In Visual Studio Code, create a file called fizz-buzz.js.

  2. Open the Output window by selecting View > Output from the menu bar.

  3. In the dropdown menu at the right of the Output window, select GitHub Copilot Log.

  4. In the editor, type:

    function fizzBuzz()
    

    GitHub Copilot should suggest code to complete the function.

  5. Accept the suggestion by pressing Tab.

    If code referencing finds a match to public code, a log entry is added to the GitHub Copilot Log view.

View code references for Copilot Chat

If a response in Copilot Chat includes matching code, this is indicated at the end of the response by the following text:

Similar code found with n license types - View matches

  1. Click View matches to display details of the matched code in a new editor tab.

    For each example of matching code, the editor displays:

    • The license type for the matching code, if known.
    • The URL of the file on GitHub.com where the matching code was found.
    • A code snippet showing the matching code.
  2. In the editor, Ctrl+click (Windows/Linux) or Command+click (Mac) a URL to view the full file on GitHub.com.

When Copilot Chat provides a response that includes code that matches code in a public GitHub repository, this is indicated beneath the code suggestion:

< > Public code references from n repositories

To see details of the matching code:

  1. Click the "Public code references..." text, under the code suggestion.

    A list of GitHub repositories containing matching code is displayed in a dropdown, together with licensing information, if found.

    Screenshot of a code completion suggestion in Copilot Chat with a link to view code references.

  2. Click the name of a repository to display that repository on GitHub.com.