Sub topic 5
Keeping files out of AI context
This chapter assumes you know basic Git, can use a command line, and understand that environment variables hold configuration. It covers how to stop an AI assistant from reading files that contain secrets.
Why exclusion matters
An AI assistant sends text to a provider. That text can include open files, search results, and anything an agent reads from disk. If a file never enters the context, it cannot be sent. Excluding files is the cheapest control you have, and it works before any other safeguard.
Exclusion has limits. It only covers paths you list. It does not stop you from pasting a secret into a prompt by hand, and it does not stop a command from printing a secret. Later chapters cover those cases.
Start from your gitignore rules
Files you keep out of Git are usually the files you want to keep out of AI context, so begin with the same list. This example covers common secret files:
.env
.env.*
!.env.example
*.pem
*.key
terraform.tfstate
terraform.tfstate.*Each line does one job:
.envand.env.*match local configuration files that hold credentials.!.env.examplere-includes the template file. It has placeholder values only, so the assistant can read it to learn which variables exist.*.pemand*.keymatch private keys and certificates.terraform.tfstateandterraform.tfstate.*match Terraform state, which can store secret values in plain text.
Use the tool's own ignore file
Many AI tools can read a separate ignore file that uses gitignore-style syntax. The file name and the level of support differ by tool, so check your tool's documentation. Do not assume a file called .aiignore or similar works everywhere.
Two things to check:
- What the ignore file blocks. Some tools block only indexing and autocomplete. Others also block file reads by an agent. Read the documentation to learn which.
- Whether
.gitignoreis respected. Some tools skip gitignored files by default. Others do not. Do not rely on this without testing it.
Test the rule
A rule you have not tested is a guess. Create a harmless dummy file and ask the assistant about it.
- Create a file such as
.envcontainingTEST_VALUE=not-a-real-secret. - Ask the assistant to report the value of
TEST_VALUE. - If it can quote the value, the rule is not working. Fix it before you put real credentials in the file.
Common errors
- The file was already open. An editor tab or earlier chat message may already have placed the content in context. Start a new session after changing the rules.
- A wrong pattern.
.envmatches that exact name. Variants such as.env.localneed.env.*. - Secrets in other places. Shell history, build logs, and exported variables are not covered by a file ignore rule.
- Committed history. Ignore rules do not remove a file that Git already tracks. Remove it from the index and treat any secret in it as exposed.
Exercise
You add .env to your tool's ignore file. Your project also has .env.local and config/prod.key. Which patterns should you add, and how do you confirm they work?
Show answer
Add .env.* (or .env.local) and *.key. Keep !.env.example if you want the template readable. To confirm, put a dummy value in each file, start a new session, and ask the assistant to quote the values. If it cannot, the rules work. If it can, the rule or the tool's support needs fixing.