Sub topic 5
Keeping Files Out of AI Context
This chapter assumes you can use Git and a terminal and have used an AI coding assistant. It also assumes you know what .gitignore does. The earlier chapters on secret types and data flow explain which files are sensitive and how they reach a provider. This chapter covers the first control: telling the tool not to read them.
What "context" means here
Context is everything the tool sends to the model with your request. It can include your prompt, open editor tabs, files the tool retrieves from an index of your repository, files an agent reads on its own, and the output of commands the agent runs. An ignore rule tries to remove files from the middle of that list: the indexed and retrieved files.
The diagram shows the main limit. The filter sits on the file path. Text you paste and command output never pass through it.
Ignore files for AI tools
Many tools read a file that looks like .gitignore and uses the same pattern syntax. The names differ by product. Examples include .cursorignore for Cursor, .aiignore for JetBrains AI Assistant, and .geminiignore for Gemini CLI. Other tools skip the file and use settings instead. Claude Code uses permission deny rules in its settings file. GitHub Copilot uses content exclusion settings that an administrator configures. Some tools have no such control at all.
Check three things in your tool's documentation before you rely on it:
- The file name or setting, and where it must live (repository root, home directory, or organization settings).
- Which features honor it. A rule may block indexing and autocomplete but not chat attachments or agent file reads.
- Whether it already respects
.gitignore. Some tools do, which gives you partial coverage for free. That coverage is unsafe for files you commit on purpose, such as an encrypted secrets file.
> The names above change between versions. Treat them as examples to look up, not as a reference.
A starting rule set
The patterns below keep common secret files out of version control. Copy the same lines into your tool's ignore file if it uses gitignore syntax.
.env
.env.*
!.env.example
*.pem
*.key
terraform.tfstate
terraform.tfstate.*Here is how the rules behave:
.envmatches the file named.envin any directory..env.*matches.env.local,.env.production, and similar names. It also matches.env.example.!.env.examplere-includes that one file. Negation only works if a later line comes after the pattern it cancels. Order matters, and the last matching line wins.*.pemand*.keymatch private keys and certificates by extension, in any directory.terraform.tfstate.*catchesterraform.tfstate.backup. State files often contain secrets in plain text.
Extend it for your stack
The starting set misses files that are specific to your project. Add what applies:
| Category | Example patterns | Why |
|---|---|---|
| Cloud credentials | `.aws/`, `credentials.json`, `service-account*.json` | Long-lived keys |
| Local config | `*.local.yaml`, `secrets.*`, `config/production.*` | Often holds passwords |
| Key stores | `*.p12`, `*.pfx`, `*.jks`, `id_rsa*` | Private key material |
| Data | `dumps/`, `*.sql`, `*.csv` in data folders | Customer records |
| Infrastructure | `.terraform/`, `*.tfvars` | Variables and cached providers |
| Proprietary code | `src/pricing/`, `vendor/licensed/` | Not a credential, but sensitive |
The last row matters because this chapter is about private code too. If a directory holds trade-secret algorithms or licensed third-party code, exclude it the same way.
Pattern rules that cause mistakes
Gitignore syntax has a few behaviors that surprise people.
- A leading slash anchors the pattern.
/secrets/matches only at the repository root.secrets/matches at any depth. - A trailing slash means directory.
dumps/ignores a directory nameddumpsbut not a file with that name. - You cannot re-include a file whose parent directory is excluded. If you write
config/and then!config/public.yaml, the second line has no effect. Exclude the files inside instead, withconfig/*followed by!config/public.yaml. - Patterns are case-sensitive on most setups. On a case-insensitive file system,
.ENVis still the same file as.env, but the pattern may not match it in every tool. - Some tools implement only part of the syntax. Negation and
**are the common gaps. Test them.
Testing your rules
Git can tell you which line in .gitignore matches a path. This confirms your patterns, but it does not confirm what the AI tool does.
git check-ignore -v .env.local config/production.yamlThe output shows the file, line number, and pattern that matched each path. A path with no output is not ignored.
To test the AI tool, create a harmless marker file that your rules should block, and ask the tool about it.
printf 'CANARY_VALUE_9f3a\n' > .env.canaryOpen a fresh chat or agent session, then ask it to quote the contents of .env.canary. If it returns the canary string, the rule does not cover that feature. Repeat the test in each mode you use: chat, inline edit, and agent. Delete the file afterward.
What ignore files do not cover
Know these gaps before you rely on the rules.
- Pasted text. If you copy a
.envfile into a prompt, no ignore rule applies. The next chapter covers redacting what you paste. - Command output. An agent that runs
cat .envorenvgets the contents through the shell, even if its file-read tool is blocked. Agent permissions, covered later, limit this. - Open files. Some tools attach the active editor tab automatically. Check whether blocked files are excluded from that behavior.
- Earlier indexing. If a file was indexed before you added the rule, the old index may still contain it. Clear or rebuild the index after you change the rules.
- Copies and links. A secret copied to
notes.txtor reached through a symlink is not matched by a rule for.env. Some tools resolve links and some do not. - Files in other repositories. Rules in one project do not protect your home directory or a sibling project that you open in the same workspace.
- Secrets inside allowed files. A hardcoded key in
app.pyis not excluded becauseapp.pyis allowed. Moving secrets out of source, covered later in this tutorial, handles this.
Layering the controls
No single control is enough, so combine them:
- Keep secrets out of the repository tree where you can. Store them in a password manager or a directory outside the project.
- Use the tool's ignore file for what remains in the tree.
- Use the tool's permission settings to deny reads of sensitive paths where it supports them.
- Run a secret scanner so a leak is caught even when the other layers fail.
Commit the ignore file so the whole team gets the same rules. Review changes to it in code review, because removing a line widens what the tool can see.
Worked example
You work on a payments service with this layout:
payments/
.env
.env.example
certs/client.pem
dumps/customers.sql
src/app.py
src/pricing/engine.pyYour tool reads .aiignore, and the team treats the pricing engine as proprietary. You write:
.env
.env.*
!.env.example
*.pem
dumps/
src/pricing/You check the Git-side behavior with git check-ignore -v, create .env.canary, and ask the assistant to quote it in chat, inline edit, and agent mode. Chat and inline edit refuse. Agent mode returns the value, because that feature reads files through a separate tool. You record this in the team's notes and turn on the agent's path-based deny rule. You repeat the test, and agent mode now refuses.
The result is a layered setup with a known gap, not a claim that the ignore file alone protects you.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Assuming `.gitignore` covers the AI tool | Files may be read anyway | Check the tool's documentation and test |
| Writing `config/` then `!config/public.yaml` | The re-include is ignored | Use `config/*` first |
| Adding rules after files were indexed | Old index still holds contents | Rebuild the index |
| Testing with a real secret | Real value leaves your machine | Use a canary value |
| Never retesting after upgrades | A new feature may skip the rules | Retest after major updates |
Exercise
Your .aiignore contains .env.* and !.env.example. A teammate reorders the file so !.env.example comes first. What happens to .env.example, and what happens to .env.production?
Show answer
With the negation first, the later .env.* line matches .env.example again, so it is excluded, because the last matching line wins. .env.production is excluded either way. The fix is to put !.env.example after .env.*.
Exercise
An agent's file-read tool is blocked from .env, yet it still shows the file's contents. Name one likely cause and one control that addresses it.
Show answer
The agent probably ran a shell command such as cat .env, which does not go through the ignore rules. Restrict command execution with approvals or deny rules for commands and paths, and keep real secrets outside the project directory.