GitHub Copilot offers code suggestions across your entire project, but sometimes you want it to stay silent in certain files. Configuration files, generated code, or third-party libraries often produce irrelevant or noisy suggestions. By restricting Copilot to specific file patterns, you can focus its assistance on the files where you actually write logic. This article explains how to set up file pattern exclusions and inclusions using a .github/copilot.yml file in your repository.
Key Takeaways: Restricting GitHub Copilot by File Pattern
- .github/copilot.yml file in the repo root: Defines inclusion and exclusion rules using glob patterns for file paths.
- Pattern syntax follows gitignore conventions: Use asterisks, question marks, and bracket ranges to match file names and directories.
- Exclusion rules override inclusion rules: If a file matches both an include and an exclude pattern, the exclude pattern takes precedence.
How GitHub Copilot File Restriction Works
GitHub Copilot checks a configuration file named copilot.yml located in the .github directory of your repository. This file uses YAML syntax and supports two top-level keys: include and exclude. Each key takes a list of glob patterns that match file paths relative to the repository root.
When you open a file, Copilot evaluates the patterns in this order:
- If the file path matches any pattern in the
excludelist, Copilot is disabled for that file regardless of inclusion rules. - If the file path matches any pattern in the
includelist and does not match any exclude pattern, Copilot is enabled. - If the file path matches neither list, Copilot falls back to the default behavior, which is enabled for all files.
This design lets you create a whitelist of files where Copilot is active or a blacklist of files where it is suppressed. You can also combine both approaches for precise control.
Prerequisites
Before setting up file pattern restrictions, confirm these requirements:
- You have a GitHub Copilot subscription for your personal account or organization.
- You have write access to the repository where you want to add the configuration file.
- Your editor or IDE has the GitHub Copilot extension installed and is signed into your GitHub account.
- The Copilot extension version is 1.40 or later for VS Code, or the equivalent recent version for JetBrains or other supported IDEs.
Steps to Create the copilot.yml File and Define Patterns
Follow these steps to add a .github/copilot.yml file to your repository and configure file pattern restrictions.
- Create the .github directory if it does not exist
Navigate to the root of your local repository. If there is no.githubfolder, create one using your file explorer or the terminal commandmkdir .github. - Create the copilot.yml file
Inside the.githubfolder, create a new file namedcopilot.yml. Use a plain text editor or your IDE. - Add include patterns to whitelist specific files
Opencopilot.ymland add anincludekey followed by a list of glob patterns. For example, to enable Copilot only in.tsand.tsxfiles under thesrcdirectory, write:include:
- 'src//ts'
- 'src//tsx' - Add exclude patterns to blacklist specific files
After the include section, add anexcludekey with patterns for files where you want Copilot disabled. For example, to block Copilot in all files inside ageneratedfolder and in.min.jsfiles, add:exclude:
- 'generated/'
- '/min.js' - Save the file and commit it to your repository
Savecopilot.yml. Stage the new file withgit add .github/copilot.yml, then commit with a message like “Add Copilot file pattern restrictions” and push the commit to your remote repository. - Verify the restrictions in your editor
Open a file that matches an exclusion pattern. The Copilot status icon in the bottom toolbar should show a disabled or muted state. Open a file that matches an inclusion pattern and confirm that suggestions appear normally.
Common Issues and Pattern Mistakes to Avoid
Copilot Still Suggests in Excluded Files
If Copilot continues to offer suggestions in files you intended to exclude, check these possibilities:
- The
copilot.ymlfile is not in the.githubdirectory. Verify the path is.github/copilot.ymlrelative to the repository root. - The file was not committed and pushed. Copilot reads the configuration from the remote repository, not your local working copy.
- The pattern syntax is incorrect. Use forward slashes for directory separators even on Windows. Verify that your glob pattern matches the exact file path.
- The exclude pattern is misspelled or uses a different case. File paths are case-sensitive on Linux and macOS, and Copilot matches them case-sensitively.
Copilot Does Not Suggest in Included Files
When inclusion patterns are defined, Copilot only activates for files that match at least one include pattern. If a file does not match any include pattern, Copilot remains silent. To fix this, review your include patterns for accuracy. For example, the pattern src/ts matches files directly in src but not in src/components. Use src//ts to match all subdirectories.
Exclude Patterns Override Include Patterns
A common misunderstanding is that include patterns take priority over exclude patterns. In reality, if a file matches both an include and an exclude pattern, the exclude pattern wins. For instance, if you include src//ts but exclude src/generated/, Copilot will not activate in src/generated/constants.ts. To avoid unexpected behavior, design your patterns so that exclude patterns cover only the files you truly want to block.
Patterns Do Not Support Negation or Comments
The glob pattern syntax in copilot.yml does not support negation patterns like !important.ts or inline comments. Each line must be a valid glob pattern. If you need to exclude most files except a few, use a broad exclude pattern and then list the exceptions as include patterns. However, remember that exclude still overrides include, so this approach works only if the exception files do not match the exclude pattern at all.
GitHub Copilot File Restriction: Include vs Exclude Patterns
| Item | Include Patterns | Exclude Patterns |
|---|---|---|
| Purpose | Whitelist files where Copilot is active | Blacklist files where Copilot is disabled |
| Default behavior | If no include patterns exist, all files are eligible | If no exclude patterns exist, no files are blocked |
| Priority | Lower priority than exclude | Higher priority than include |
| Common use case | Limit Copilot to source code in specific folders | Block Copilot in generated files, configs, or third-party code |
| Pattern examples | src//py or lib/rb |
dist/ or /lock |
You can now control exactly where GitHub Copilot provides suggestions in your repository by using include and exclude patterns in the .github/copilot.yml file. Start by identifying the file types or directories that produce the most noise, then add corresponding exclude patterns. For projects with many non-source files, consider using include patterns to restrict Copilot to your primary code directories. Remember to commit and push the configuration file so that all team members benefit from the same restrictions. As a final tip, test your patterns with a small set of files first to confirm the behavior before rolling out to the entire repository.