Gitignore and AI coding: check tracked files before sharing

gitai-codingprivacyworkflow

TL;DR: Adding a filename to .gitignore does not remove a file already tracked by Git, and it does not define an AI tool’s access permissions. Check tracking and ignore rules separately, then review an explicit context list. Use a disposable repository to learn the commands before changing a real project’s index.

What you’ll be able to do:

  • Explain why a tracked configuration file still appears after adding an ignore rule.
  • Find the ignore pattern that applies without printing file contents.
  • Separate Git cleanup from decisions about an AI tool’s readable context.

Honest take: Treat .gitignore as repository hygiene and use a reviewed file list for AI context; neither replaces access controls.

Why does an ignored file still appear in a Git diff?

An ignored file can still appear in a Git diff because ignore rules do not apply to files already tracked in the index. The official gitignore manual makes that boundary explicit. Adding a rule changes how Git handles matching untracked paths; it does not rewrite the existing index or history.

This matters when an AI coding assistant edits a project configuration. You notice a local settings file in the patch, add its name to .gitignore, and expect the next diff to omit it. The file remains tracked, so the change is still part of the working-tree comparison. Nothing about that sequence proves the ignore pattern was misspelled.

Keep three questions separate: is the path tracked, does an ignore pattern match it, and can the tool read it? Git answers the first two. Your tool’s configuration and operating environment determine the third. This article makes no claim that every editor, agent, or repository indexer follows Git’s exclude rules.

For a coding review, that distinction prevents a misleading reassurance: “Git ignores it” is not enough evidence that a file was excluded from a prompt, a terminal read, an attachment, or a previously created artifact. Check the actual context construction path before sharing anything sensitive.

Which commands answer which question?

Use tracking and ignore commands together rather than trying to infer everything from git status. Run them from the repository root and substitute only a path you intend to inspect. The examples below report filenames and matching patterns, without reading file contents.

CommandQuestion it answersImportant boundary
git ls-files --error-unmatch -- app.localIs this exact path represented in the index?Does not prove it is safe to share
git check-ignore -v -- app.localDoes normal ignore processing report this path?Tracked paths are normally omitted
git check-ignore -v --no-index -- app.localWhich ignore rule would apply independent of tracking?Does not remove the path from the index
git status --short --ignored -- app.localWhat is the current status, including ignored paths?A filename view, not a disclosure audit

The git-check-ignore manual documents the --no-index behavior and verbose output. A matching rule reported with that option and a successful git ls-files lookup can both be true. They are not contradictory results.

For scripts, interpret exit codes deliberately. git check-ignore uses a nonzero status when no path is ignored; that result is not the same as an execution failure. A pipeline running with shell error-exit settings can stop at that expected branch unless it handles it explicitly. Do not suppress every error just to keep a script moving.

How can you reproduce the tracked-file trap safely?

Create a fresh scratch directory that does not already exist. The following POSIX shell sequence uses only fake settings and a local demo commit. It has no remote and never reads a real environment file. If mkdir fails, stop instead of running the remaining commands in a different directory.

mkdir gitignore-context-demo
cd gitignore-context-demo
git init -q
git config user.name 'Local Demo'
git config user.email 'demo@example.invalid'
printf 'mode=demo\n' > app.local
git add -- app.local
git commit -qm 'Track synthetic local settings'
printf 'app.local\n' > .gitignore
printf 'mode=changed\n' > app.local
git ls-files --error-unmatch -- app.local
git status --short -- app.local
git check-ignore -v --no-index -- app.local

The demonstrated outputs are:

app.local
 M app.local
.gitignore:1:app.local app.local

The final line is shown with whitespace normalized for readability; Git separates the pattern and path with a tab. app.local is tracked, has a working-tree modification, and matches the newly added rule when index filtering is disabled. The rule is functioning, but it does not do what you initially wanted.

Now run normal ignore checking as a separate command:

git check-ignore -v -- app.local

In this fixture it prints nothing and exits with status 1. That is the expected non-match result for normal processing of the tracked path. Save the status before issuing another shell command if you need it for an automated assertion.

The demonstration was executed with Git 2.47.3 on November 10, 2026. It is a Git behavior fixture, not a comparison of AI models or a claim that a particular coding product exposed private data. The exact transcript, including whitespace and return codes, is more useful evidence than an invented model conversation.

What changes when the file becomes untracked?

Removing a path from the index while keeping its working copy allows the ignore rule to govern its ordinary untracked status. In the scratch repository only, continue with:

git rm --cached -- app.local
git ls-files -- app.local
git status --short --ignored -- app.local
git check-ignore -v -- app.local
test -f app.local && printf 'working copy retained\n'

git ls-files now prints no path. Status shows a staged deletion relative to the earlier commit and an ignored working copy. Normal git check-ignore reports the rule, and the final command confirms the file still exists. The git-rm manual documents that --cached removes paths from the index while leaving working-tree files alone.

That is a demonstration, not a command to paste across a real repository. Inspect the index and working-tree changes first, preserve needed configuration, and coordinate any tracking change with the people who use the repository. A staged deletion is a proposed repository change; it is not a local privacy toggle.

If Git refuses the removal because the staged and working versions differ, investigate the versions rather than adding a force flag. A refusal may be protecting work you have not reviewed. The tutorial’s fixture starts from a known state precisely so that the index operation is understandable.

Does removing a file from tracking remove past copies?

Removing a path from the current index does not erase earlier commits. Anyone with an older copy may still have it, and a previously shared patch or context bundle is separate from the current working directory. Avoid presenting the cleanup as retroactive protection.

If a real credential has been disclosed, the account owner’s response must address the credential and exposure, not merely the repository’s current filename list. That situation is outside this synthetic demo. Do not paste the suspected secret into an AI chat to ask whether it looks sensitive.

There is also an application-design question: should the project contain a public template instead of a machine-specific configuration? A template with placeholders can document required keys without keeping local values tracked. Review that template as ordinary source code; an example filename does not guarantee its contents are safe.

How should you build a context list for an AI review?

Build the file list first and inspect it before reading or packaging contents. The git-ls-files reference distinguishes cached paths from untracked paths and supports standard exclude rules for the latter. These categories help inventory a repository, but the result is still a candidate list requiring review.

git ls-files -z > tracked-paths.zlist
git ls-files --others --exclude-standard -z > untracked-paths.zlist

These commands write path lists into the current directory. In a real workflow, place them in a deliberate scratch location outside the source tree so the inventory does not become a new context candidate. NUL separation preserves filenames containing whitespace and newlines; do not feed the result to a line-based loop and assume every path survived unchanged.

A practical policy is an explicit allowlist: select the source modules, tests, and public configuration needed for the question, then exclude private settings and unrelated artifacts regardless of tracking status. Review additions as carefully as removals. A generated report may contain material copied from an otherwise excluded file.

For a patch review, use our staged and untracked review guide to define what is being reviewed. For an agent that edits files, the file-scope guard tutorial addresses unexpected modifications. An edit boundary and a read boundary are different policies; neither should be inferred from the other.

Why is a clean status an incomplete privacy check?

A clean Git status means there are no changes reported under that command’s view and options. It does not mean every tracked file is suitable for external processing. A configuration committed long ago can be unchanged today and still be inappropriate context.

Conversely, an ignored file may be entirely harmless, such as a disposable build cache. The ignored category is not a secret classification. The git-status manual documents how ignored and untracked paths appear when requested; it does not promise anything about external tooling.

Write the share decision down in terms of paths and purpose: which files were selected, which source revision they came from, and what question requires them. Do not label the entire repository “safe” based on one filename command. If the selected list changes, review the new list before creating the next bundle.

Honest take: when should you avoid changing .gitignore?

Avoid changing .gitignore when the problem is simply that an AI task needs less context. Select fewer files instead. Repository ignore rules affect developer workflows beyond the current task, including how new files appear to contributors. A broad pattern added for one review can hide useful future additions from ordinary status output.

Shared ignore rules belong in the repository when they express a project-wide convention. Local exclusions and global excludes have different scope; consult the gitignore documentation before deciding where a rule belongs. Use verbose rule provenance rather than guessing which configuration file won.

For a one-off review, the smallest sufficient context list is easier to audit than a repository-wide cleanup. It also gives you a clearer explanation of what the assistant was able to see, without inventing token savings or subscription-cost reductions from a file count.

FAQ

Q: Why does --no-index report a rule when normal check-ignore is silent? A: Normal checking skips tracked paths. --no-index lets you investigate matching rules independently of that filtering; it does not change tracking.

Q: Does .gitignore prevent an AI tool from reading a file? A: It defines Git exclusion patterns. Check the specific tool and execution environment for read restrictions rather than assuming those restrictions follow Git.

Q: Can I use git rm --cached as a universal fix? A: No. It stages a repository change and leaves a local file. Review the index, working copy, and collaboration consequences before using it outside the demo.

Sources

Last updated November 10, 2026. Official Git references and a synthetic local repository were checked on this date; no AI-provider privacy audit or model benchmark was performed.

Was this article helpful?

Know which coding tool is worth paying for

Hands-on comparisons of AI coding assistants and what each one costs to run — including the local-model path. Sent only when something changes. Unsubscribe anytime.