Use .clineignore to keep files out of Cline's context
Read what .clineignore actually filters, write patterns that hold, check the effect, and know @ mentions still read ignored files.
Before you start · 1. Sort paths into two lists · 2. Create .clineignore at the workspace root
At a glance
A .clineignore file at your project root tells Cline which files and directories to skip while analysing your codebase, using .gitignore syntax. What it saves is context: dependencies, build outputs and coverage reports pulled in automatically take up tokens and push useful material out of the window. The official documentation marks the feature as being deprecated soon. It also filters automatic loading only, so it is not a security or access-control boundary: ignored files can still be read through an explicit @ mention or a shell command. This tutorial first gets the filter right, then covers the official sample hook for intercepting matching file-tool calls.
Workflow
It is a filter, not a permission boundary
Matching files are excluded from the file listing Cline sees when starting a task, from automatic context gathering during a conversation, and from search results. An explicit @ mention still reads them, for example @/node_modules/some-package/index.js.
Pattern syntax follows .gitignore
Directories, globs, ! negations, # comments and blank lines all follow .gitignore semantics: /build/ matches only the root build directory, **/node_modules/ matches at any depth, and !
important.csvis an exception.The feature is being phased out
The same official page offers the replacement route: a PreToolUse hook that turns the same .clineignore file into a restriction that actually blocks tool calls.
Before you start
- A project workspace. Put .clineignore at the workspace root; in a multi-root workspace each root can have its own copy.
- A sorted list of paths: what Cline should skip (dependencies, build outputs, coverage, large data files, generated code, environment files holding secrets) and what it must still read (source you are editing, configuration such as
tsconfig.jsonandpackage.json, READMEs, test files). - A harmless placeholder file for the verification step, such as config/.env.production containing only fake values. Do not demonstrate with real secrets.
- For the enforced hook later: jq and git available (git is used only as a pattern matcher, so the workspace need not be a Git repository).
1. Sort paths into two lists
The documentation's usual exclusions: package manager directories (node_modules/, vendor/, .venv/), build outputs (dist/, build/, .next/, out/), coverage reports (coverage/), lock files when large, large data files (.csv, .xlsx, .sqlite, .parquet), binary assets, generated code (API clients, protobuf outputs, minified bundles) and environment files holding secrets. Keep readable: source you actively work on, configuration Cline needs, documentation and READMEs, and test files.
2. Create .clineignore at the workspace root
The file is named .clineignore, not .gitignore. They are independent: files tracked by Git but irrelevant to Cline, such as large test data, belong in .clineignore even when they never appear in .gitignore.
3. Write the sensitive entries first, then the bulk entries
Put environment files, certificates and private directories at the top under a comment: .env, .env.*, secrets/, *.pem. Start a separate block for dependencies and build outputs. Grouped comments let you tell later which line is about safety and which is only about context size, so a cleanup edit does not delete the wrong one.
4. Get the patterns right with .gitignore semantics
node_modules/ matches that directory; **/node_modules/ matches it at any depth; /build/ matches only the root; *.csv matches every CSV; a line starting with ! is an exception; # starts a comment and blank lines are ignored. After writing, read each line back and ask what it is meant to exclude before deciding whether it needs a leading slash.
5. Start a new task and look at the effect
Matching paths should disappear from the file listing Cline sees when starting a task, from automatic context gathering, and from search results. Also note token usage in the task header for comparison; the documentation suggests looking at it, but do not require it to change. Completion rests on the exclusions and exceptions behaving as intended. If Cline says context about a file is missing, check whether one of your own patterns excluded it.
6. Check the legacy filter boundary
Explicitly @ mention the harmless placeholder file you prepared and confirm it is still read. That is the boundary of this kind of filter: it governs automatic loading, not access. Writing .clineignore does not mean the file cannot be read, so do not rely on it alone for genuinely secret material.
7. Intercept matching file calls with the hook
The documentation says .clineignore is being deprecated and gives the enforced route. In the extension, hooks live in .clinerules/hooks/, must be named exactly PreToolUse with no extension, and need a shebang and the executable bit; then check Enable Hooks in Cline's feature settings. The documented commands are exactly: mkdir -p .clinerules/hooks
curl -o .clinerules/hooks/PreToolUse https://raw.githubusercontent.com/cline/cline/main/sdk/examples/hooks/PreToolUse_ClineignoreGuard.sh
chmod +x .clinerules/hooks/PreToolUse
In the CLI the path is .cline/hooks/PreToolUse.sh, also made executable with chmod +x. On a match the hook cancels the tool call and stops the current task run: the extension shows the PreToolUse row followed by Aborted, the CLI run ends, and a follow-up message continues the conversation. To stop using the hook, remove the hook file. The sample hook reads only workspaceRoots[0] and passes paths outside that root. Legacy-filter guidance for multiple roots does not establish hook coverage of every root. After enabling it, request a matching read of a dummy-value file and inspect the cancellation reason and extension Aborted state or CLI run ending. Send a follow-up message to continue. The hook does not filter searches or file lists, and its checks do not require matching files to remain readable by @.
Original example
Synthetic example, not a test run. A repository has config/.env.production (containing only fake values, such as one line DUMMY_KEY=not-a-real-secret), data/orders-2025.csv (assume it is large), samples/tiny.csv containing dummy data, and node_modules/. The .clineignore at the workspace root could read:
# environment and keys
.env
.env.*
secrets/
*.pem
# dependencies and build output
node_modules/
**/node_modules/
/dist/
/coverage/
# large data and generated code
*.csv
!samples/tiny.csv
*.min.js
*.map
Expected result: when a task starts, those paths no longer appear in Cline's file listing, automatic context or search results, while samples/tiny.csv is not excluded by these patterns and remains available for reading thanks to the ! exception, which lets Cline understand the CSV columns. Then point an explicit @ mention at config/.env.production (fake values only) and confirm it is read through the explicit mention even though config/.env.production matches .env.* for automatic exclusion. Explicit @ mentions can bypass the filter, so real secrets need protection beyond this layer.
The preceding outcomes test only the legacy filter. If the hook is enabled, the matching dummy-file read should be cancelled; use the separate hook checks.
Completion checks
- Legacy filter: check automatic exclusions, search and file lists in a new task; record token usage without requiring a change.
- Legacy filter: explicitly @ mention the dummy file and confirm it remains readable; this provides no access protection.
- Legacy filter:
samples/tiny.csvis not excluded; explicitly request its read and check it. Removing an exclusion does not guarantee automatic context inclusion. - Multiple roots: configure legacy filters per root; the sample hook covers matching file calls only within the first root.
- Hook: after enabling, request a matching dummy-file read, inspect cancellation and Aborted or run ending, then send a follow-up. Do not require hidden search/list results or a successful @ read.
Limits
- The official documentation states .clineignore will be deprecated soon, and that it filters automatic loading without being a security or access-control boundary: ignored files remain readable through an explicit @ mention or a shell command.
- The hook is not complete either: it does not filter search or file-listing results, its shell guard is a conservative token check rather than a full shell parser, and paths are canonicalised lexically without resolving symlinks, so add any alias pointing at an ignored file to .clineignore too.
- In the CLI, hooks are disabled in --yolo mode; use --act or --plan.
- A pattern can exclude source, configuration or test files you wanted Cline to see; when it seems to lack context, check your exclusion patterns first.
- The sample hook reads only workspaceRoots[0] and passes paths outside that root. Legacy-filter guidance for multiple roots does not establish hook coverage of every root. After enabling it, request a matching read of a dummy-value file and inspect the cancellation reason and extension Aborted state or CLI run ending. Send a follow-up message to continue. The hook does not filter searches or file lists, and its checks do not require matching files to remain readable by @.
Verification scope
Written from the current official .clineignore documentation, including its deprecation note and hook section, which were read for this page. Sign-in, installation, running the product API, file uploads and generated output were not tested. Paths and patterns in the sample are synthetic, and the hook install commands are copied from the official page but were not executed here, so they are not verified as working.
Article sources
Original editorial content; official descriptions and checking suggestions are distinguished.