Sub topic 10
Add custom instructions to a repository
This chapter assumes you have Copilot installed and signed in, and that you can use the Chat view and attach context to a request. You should also be comfortable with Markdown and with committing files to a Git repository.
Why custom instructions exist
Without guidance, Copilot Chat falls back on general habits. It may use default exports when your team prefers named ones, or write tests in a framework you don't use. You could repeat these rules in every prompt, but that is slow and easy to forget.
Custom instructions solve this. They are a Markdown file in your repository that Copilot Chat reads and adds to chat requests. The rules travel with the code, so everyone who clones the repository gets the same guidance.
How the file works
Copilot looks for one specific file in the workspace:
.github/copilot-instructions.mdThe file is plain Markdown. You write short natural-language rules, and Copilot includes them as extra context when you chat. It does not run the file as code or enforce it as a linter. The rules are guidance that the model usually follows, but not always.
Create the file
- Open your repository folder in VS Code.
- Create a
.githubfolder at the repository root if it does not exist. - Inside it, create a file named
copilot-instructions.md. - Add your rules and save.
Here is a small example for a TypeScript project:
# Copilot instructions
- Use TypeScript with strict typing; avoid `any`.
- Write unit tests with Vitest.
- Prefer named exports over default exports.Commit the file so teammates receive it. Because it lives in version control, changes to the rules go through the same review process as code.
Write rules that work
Good instructions are short, specific, and checkable. Compare these:
| Weak rule | Better rule |
|---|---|
| Write good code. | Use TypeScript with strict typing; avoid `any`. |
| Test things. | Write unit tests with Vitest. |
| Follow our style. | Prefer named exports over default exports. |
Some practical guidelines:
- State project facts Copilot cannot infer, such as the test framework or the module style.
- Keep each rule to one line so it is easy to review.
- Avoid rules that contradict each other.
- Skip long essays. A short file is easier to maintain and leaves more room for the rest of your request.
Check that the instructions are used
Open the Chat view and ask for something the rules should affect. For example:
Explain what #file:utils.ts does and suggest one way to simplify it.Then ask Copilot to add a function and a test for it. If your rules say to use Vitest and named exports, the generated code should reflect that. In VS Code, the response usually lists the references it used, and the instructions file may appear among them. If you don't see the file listed, the first things to check are the file name and location.
Common problems
- Rules seem ignored. Check that the path is exactly
.github/copilot-instructions.mdat the repository root of the folder you opened. A typo or a nested location means Copilot won't find it. - Wrong folder opened. If you open a subfolder of the repository, the root
.githubfolder is outside your workspace. - Vague or conflicting wording. The model may pick one rule over another. Rewrite the rules to be specific.
- Expecting enforcement. Instructions guide generation. Keep your linter and tests in place to catch violations.
Exercise
Where must the custom instructions file be saved, and why is it a good idea to commit it?
Show answer
Save it as .github/copilot-instructions.md at the repository root. Committing it shares the same rules with everyone who clones the repository and lets changes be reviewed like code.
Exercise
Copilot keeps generating default exports even though your instructions file says to prefer named exports. Name two things to check.
Show answer
Check that the file name and path are exactly .github/copilot-instructions.md at the root of the folder opened in VS Code. Also check that the rule is specific and does not conflict with another rule. Remember that instructions are guidance, not enforcement.