GitHub Copilot in VS Code can suggest code based on files you do not want it to read. When Copilot ignores your .gitignore rules, it may pull context from sensitive or irrelevant files. This happens because Copilot by default indexes all open files and project files regardless of .gitignore. This article explains how to configure Copilot to respect .gitignore and keep its suggestions focused on the files you actually want it to see.
Key Takeaways: Make GitHub Copilot Respect .gitignore
- VS Code Settings > GitHub Copilot > Ignored Files: Add file patterns to prevent Copilot from indexing those files.
- .github/copilot-instructions.md: Write instructions that tell Copilot to exclude .gitignore patterns.
- Copilot Chat > /reset: Clear Copilot’s context cache after changing ignore settings to force it to re-evaluate files.
Why GitHub Copilot May Ignore Your .gitignore
GitHub Copilot by default does not read .gitignore rules. It indexes all files in the workspace that are not explicitly excluded by VS Code’s file exclusion settings. This means files like .env, node_modules, build artifacts, and configuration secrets may be included in Copilot’s context. The root cause is that Copilot’s indexing engine operates independently from Git’s file tracking. Copilot uses its own file inclusion logic based on the VS Code workspace and the open editor tabs. When you open a file that is gitignored, Copilot indexes it immediately. This can lead to suggestions that reference code from files you never intended to expose to Copilot.
Steps to Configure Copilot to Respect .gitignore
Follow these steps to restrict Copilot’s file indexing to only the files you want.
- Open VS Code Settings
Press Ctrl + , to open the Settings editor. Alternatively, go to File > Preferences > Settings. - Search for Copilot Ignored Files
In the search bar at the top, typeignored files. The setting GitHub Copilot: Ignored Files appears under Extensions > GitHub Copilot. - Add File Patterns to Ignore
Click Add Item and enter a glob pattern for each file or folder you want Copilot to skip. Common patterns include.env,node_modules/,dist/,.git/, andlog. Each pattern must match the exact syntax used in .gitignore. Press Enter after each pattern. - Create a Copilot Instructions File
In the root of your project, create a folder named.githuband inside it create a file namedcopilot-instructions.md. Add the following content:You must ignore all files that match patterns in the project's .gitignore file. Do not consider .env files, build output, dependencies, or any file listed in .gitignore as context for suggestions.
Save the file. Copilot reads this file on the next suggestion request. - Reload VS Code Window
Open the Command Palette with Ctrl + Shift + P, typeDeveloper: Reload Window, and press Enter. This clears Copilot’s internal cache and forces it to apply the new ignore patterns and instructions. - Test the Configuration
Open a file that is listed in your .gitignore, such as.env. Type a few characters and see if Copilot offers suggestions. If the configuration worked, Copilot should not provide any suggestions based on that file. Then open a source code file that is not gitignored and verify that suggestions still appear.
If Copilot Still Reads Ignored Files
Copilot Suggests Code From node_modules or Build Folders
If Copilot still references files you excluded, the issue is likely that Copilot has cached older index data. Run the command GitHub Copilot: Clear Index from the Command Palette. Wait a few seconds and try again. If the problem persists, add the specific folder path to the Ignored Files setting using an absolute pattern like /node_modules/ instead of node_modules/. The leading slash ensures the pattern matches only the root folder.
Copilot Suggests Secrets From .env Files
If you see API keys or passwords in Copilot suggestions, immediately stop using Copilot until you have applied the ignore patterns. Add .env and .env. to the Ignored Files setting. Also add a line to your copilot-instructions.md that says Never suggest content from .env files or any file containing secrets. After making these changes, clear the index and reload the window. Check your Git history to confirm no sensitive data was committed.
Copilot Reads Files From Other Workspace Folders
If you have multiple folders in your VS Code workspace, Copilot may index files from all of them. To restrict Copilot to only the current project, open the workspace settings file (.vscode/settings.json) and add the ignored patterns there. Workspace settings override user settings. For example:"github.copilot.ignoredFiles": [".env", "node_modules/", "dist/"]
Save the file and reload the window.
| Item | VS Code Settings Method | Copilot Instructions Method |
|---|---|---|
| Description | Manually list file patterns in VS Code settings | Write natural language rules in a markdown file |
| Configuration location | Settings > GitHub Copilot > Ignored Files | .github/copilot-instructions.md in project root |
| Scope | Global or workspace | Per repository |
| Pattern syntax | Glob patterns | Plain English instructions |
| Effectiveness | Directly blocks file indexing | Guides Copilot behavior but may not block indexing |
| Best for | Blocking sensitive or large folders | Fine-tuning suggestion relevance |
You can now configure GitHub Copilot to ignore files listed in .gitignore using both VS Code settings and a copilot-instructions.md file. Start by adding the most critical patterns like .env and node_modules. After each change, clear the Copilot index and reload the window to ensure the new rules take effect. For teams, consider committing the copilot-instructions.md file to your repository so all members benefit from the same rules. An advanced tip: use the github.copilot.ignoredFiles setting in your workspace settings.json to enforce ignore rules across your entire team without relying on each developer’s user settings.