GitHub Copilot offers repository custom instructions that allow you to provide additional context for the work it does in your repository. You can create a special file (.github/copilot-instructions.md) that helps Copilot generate higher quality responses tailored to your project needs.
Copilot primarily learns from the context of your current file, open tabs, and your overall project structure. Here's how you can guide it:
You can embed "custom instructions" or preferences within comments, especially when starting a new block of code or asking Copilot Chat for assistance.
Example: Guiding Copilot with Comments:
// Task: Create a function to fetch user data from '/api/users/{id}'
// Requirements:
// - Use the 'axios' library for the GET request.
// - Implement error handling for network issues and non-200 responses.
// - Return the user data object on success, or null on failure.
// - Adhere to async/await syntax.
async function fetchUserData(userId) {
// Copilot will attempt to generate code based on the comments above
// and the function signature.
}
In Copilot Chat, you can be more explicit:
Write a Python function called `process_data`.
It should take a list of dictionaries as input.
For each dictionary, extract the 'value' key and multiply it by 2.
Return a new list containing these modified values.
Please ensure the function includes a docstring explaining its purpose, arguments, and return value.
Only use standard Python libraries.
GitHub Copilot can provide chat responses that are tailored to the way your team works, the tools you use, or the specifics of your project, if you provide it with enough context. Instead of repeatedly adding this contextual detail to your chat questions, you can create a file in your repository that automatically adds this information for you.
The additional information is not displayed in the chat, but is available to Copilot to allow it to generate higher quality responses.
Follow these steps to add custom instructions to your repository:
Example of a custom instructions file:
We use Bazel for managing our Java dependencies, not Maven, so when talking about Java packages, always give me instructions and code samples that use Bazel.
Please format all code examples according to Google Java Style Guide.
When responding to questions about our API, note that we're in the process of migrating from v1 to v2, and you should recommend v2 endpoints where available.
The instructions you add to the .github/copilot-instructions.md file should be short, self-contained statements that add context or relevant information to supplement users' chat questions.
Consider the size and complexity of your repository. The following types of instructions may work for a small repository with only a few contributors, but for a large and diverse repository, they may cause problems with other areas of Copilot:
Examples of effective instructions:
The instructions in the .github/copilot-instructions.md file are available for use by Copilot Chat as soon as you save the file. The complete set of instructions will be automatically added to chat prompts that relate to the repository containing the instructions file.
In Copilot Chat's immersive view, you can start a conversation that uses repository custom instructions by adding, as an attachment, the repository that contains the instructions file.
Whenever repository custom instructions are used by Copilot Chat, the instructions file is added as a reference for the response that's generated. To find out whether repository custom instructions were used, expand the list of references at the top of a chat response in the Chat panel and check whether the .github/copilot-instructions.md file is listed.
For organizations using GitHub Copilot Enterprise, there might be more advanced options for fine-tuning models on your private codebase or defining organization-wide policies. Refer to the specific GitHub Copilot Enterprise documentation for such features.