Writing GitHub Copilot Instruction Files
This lesson explains repository-wide, path-specific and agent instruction files and how to write effective SQL guidance for Copilot.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish between copilot-instructions.md, .instructions.md files and AGENTS.md.
- Explain how applyTo globs and precedence work.
- Identify what makes instructions effective and safe.
Types of instruction files
GitHub Copilot reads several kinds of instruction files from a repository, summarized in the following table.
| File | Location | Applies to |
|---|---|---|
| copilot-instructions.md | .github/copilot-instructions.md | Every chat request in the repository |
| NAME.instructions.md | .github/instructions/ (with applyTo front matter) | Files matching the applyTo glob patterns |
| AGENTS.md | Anywhere; the nearest file in the tree wins | Agents working in that part of the repository |
When both a repository-wide file and matching path-specific files apply, Copilot uses all of them. Path-specific files can exclude Copilot code review or the cloud agent with excludeAgent. Personal instructions take precedence over repository instructions, which take precedence over organization instructions.
Instruction files
A path-specific SQL instruction file
---
applyTo: "database/**/*.sql"
---
# T-SQL conventions for this repository
- Target platform: Azure SQL Database, compatibility level 170.
- Use schema-qualified two-part names (Sales.Orders), PascalCase object names.
- Stored procedures start with SET NOCOUNT ON and SET XACT_ABORT ON,
use TRY...CATCH, and re-raise errors with THROW.
- Build dynamic SQL only with sp_executesql and parameters; wrap identifiers in QUOTENAME.
- Never use SELECT *, NOLOCK hints or hard-coded credentials.
- New tables need a primary key, explicit constraint names, and datetime2 for times (UTC).
- Vector columns use vector(1536) with the text-embedding-3-small model.The file lives in .github/instructions/sql.instructions.md, so these rules apply only when Copilot works with SQL files under the database folder, while C# files can have their own instruction file.
Scoping instructions
Writing effective instructions
Instructions work best when they are short, specific and verifiable: the target platform and version, naming conventions, required patterns and banned patterns, as in the example. Long essays or vague goals such as "write good SQL" add tokens without changing behaviour. Instruction files are part of the codebase, so they are reviewed in pull requests, kept current when conventions change, and never contain secrets, connection strings or personal data. In VS Code, the references list of a chat response shows whether an instruction file was used.
A useful instruction?
Different rules for SQL and C#
A repository contains an ASP.NET API and a SQL database project. The team wants Copilot to follow T-SQL conventions only for .sql files and C# conventions only for .cs files, plus one general rule for the whole repository. How should the instructions be organized?
Put the general rule in .github/copilot-instructions.md, and create .github/instructions/sql.instructions.md with applyTo: "**/*.sql" and .github/instructions/csharp.instructions.md with applyTo: "**/*.cs". Copilot combines the repository file with the matching path-specific file. Putting everything in one repository-wide file would apply SQL rules to C# work, and personal instructions would not be shared with the team.
A repository has both .github/copilot-instructions.md and a matching sql.instructions.md. Which does Copilot use?
Show answer
Both: the repository-wide file and every matching path-specific file are applied together.
Exam Tips
Remember for the exam
- Repository-wide: .github/copilot-instructions.md.
- Path-specific: .github/instructions/*.instructions.md with applyTo globs.
- Both apply when they match; personal instructions take precedence over repository ones.
- Keep instructions short, specific and free of secrets.