All guides

Original explainer

GitHub Copilot path-specific instructions: different rules per directory with applyTo

Split instructions into files under .github/instructions, scope them with globs, and check path matches before committing.

AI Nav CityWritten / Updated Sources checked

Repository-wide instructions apply to every file, yet frontend and backend rules often differ. This guide creates one path-specific file for TypeScript under src/web and one for Python under services, and confirms they do not overlap by accident. You need write access to a repository and use the Copilot cloud agent or code review that the official page names; for code review, also confirm custom instructions are not turned off in repository or personal settings. The files are written from that page and path matches were checked with local glob engines. Nothing was committed to GitHub and Copilot was not triggered.

Action and acceptance sequence

  1. Split the scope

    Group rules by directory or language and keep only general ones repository-wide.[1]

  2. Write applyTo

    End the file name with .instructions.md and scope it with globs in frontmatter, comma-separated.[1]

  3. Check matches

    Use sample paths to see which files match and whether they overlap.[1]

Check each stage separately; the final section states execution scope.

Tell the three kinds apart

The official page lists three kinds of repository instructions: .github/copilot-instructions.md applies to all requests in the repository; NAME.instructions.md files under .github/instructions apply only to files matching their paths; AGENTS.md is for AI agents, and the nearest one in the directory tree takes precedence. When a path-specific file matches and a repository-wide file exists, both are used. Keep general conventions repository-wide and write directory-specific rules into path files. Original exercise: the web rules cover only .ts and .tsx under src/web, and the Python rules cover only .py under services.[1]

Write two path-specific files

Create two files under .github/instructions; their names must end with .instructions.md, and subdirectories are allowed. In web-ts.instructions.md the frontmatter reads applyTo: "src/web/**/*.ts,src/web/**/*.tsx", with multiple globs separated by commas; python-services.instructions.md reads applyTo: "services/**/*.py". Write the instructions in plain Markdown, for example use named exports in src/web, or add type hints to public functions. To keep a file away from one feature, add excludeAgent, whose only documented values are code-review and cloud-agent.[1]

Check globs against sample paths

The official page states that * matches only the current directory and ** matches all directories: src/*.py matches src/foo.py but not src/foo/bar.py. Do not write globs from memory; check the 8 sample paths listed below one by one. Add a file whose applyTo is **/*.ts and .ts files such as src/web/ui/Button.test.ts and src/web/app.ts match two files at once. The page does not say which of two same-kind instructions wins in a conflict, so keep them from contradicting each other.[1]

  • src/web/app.ts: matches web
  • src/web/ui/Button.tsx: matches web
  • src/web/ui/Button.test.ts: matches web (.ts)
  • services/billing/invoice.py: matches Python
  • services/main.py: matches Python
  • src/server/api.ts: matches no path file
  • tools/gen.py: matches no path file
  • tests/test_invoice.py: matches no path file

Scope and stop conditions

Two local glob implementations, picomatch and Node path.matchesGlob, were compared with 19 outcomes derivable from the glob description on the official page (plus 2 comma-separated cases) and the 8 exercise paths; all agreed, and the frontmatter parses as YAML. This shows the glob spelling matches the page, not that GitHub uses the same engine, and Copilot was not triggered. The page says path-specific instructions on GitHub.com currently support only the Copilot cloud agent and code review, and that code review reads instructions from the pull request head branch, so you can try changes inside the same PR. If results do not reflect a rule, check the path, file name and frontmatter before adding rules. The page does not say where to see evidence of path-specific use, so that is unverified.[1]

Article sources

Original editorial content; official descriptions and checking suggestions are distinguished.

  1. GitHub Copilot · Official sources
    View details