Practical technical guides / Recent development (gemini-cli PR 30370, merged 19 November 2026)

Pruning Ignored Directories Can Skip a Nested .gitignore That Re-Includes Files

Why turning dir/** into a pruned directory hides files a nested .gitignore re-includes, checked against real git, and a pruning rule that keeps the speed-up only where it is safe.

By Sandeep Belgavi · Published 2026-11-24 · Prepared 2026-11-23

Why turning dir/** into a pruned directory hides files a nested .gitignore re-includes, checked against real git, and a pruning rule that keeps the speed-up only where it is safe.

The bug report

Coding agents and their command-line tools walk a repository to decide which files the model may read or search. Most honour .gitignore, so that build output and dependencies stay out of the model's context. Walking a large repository is slow, so tools prune: when a directory is known to be fully ignored, they skip it without listing its contents.

Gemini CLI's pull request 30370, merged on 19 November 2026, fixes a case where that pruning was wrong. The pull request describes it precisely: with logs/** in the root .gitignore and !keep.txt in logs/.gitignore, the parser ignored logs/keep.txt, which git does not. An earlier change had turned wildcard patterns such as dir/** into a synthetic directory pattern /dir/, and the parser then pruned the directory "without loading <dir>/.gitignore, ignoring any negation rules (!) inside it". The fix keeps the synthetic pattern only when the nested .gitignore has no negation, preserving the speed-up for the common cases such as node_modules/**.

At the time of writing the fix is in the nightly builds from v0.71.0-nightly.20261120 onwards. It is not in the stable v0.70.0 or in v0.71.0-preview.0, both published on 19 November before the merge. This guide explains why the pruning was wrong in terms of git's own rules, checks the expected answer against real git, and implements a pruning rule that is safe for the cases it covers.

What git actually does with dir/**

The git documentation for .gitignore says that a trailing /** matches everything inside a directory. The key word is inside: logs/** matches logs/a.txt and logs/sub/, but not logs itself. So git does not exclude the logs directory. It enters it, reads logs/.gitignore, and applies its rules to the files there. A later rule wins over an earlier one, so !keep.txt in logs/.gitignore re-includes logs/keep.txt.

The same documentation has the rule that limits how far that goes: "It is not possible to re-include a file if a parent directory of that file is excluded." logs/sub/ is inside logs, so logs/** excludes it as a directory, and git does not enter it. A keep.txt in logs/sub/ stays ignored even though !keep.txt would match its name.

logs/ with a trailing slash and no stars is different again: it excludes the directory itself, git never reads logs/.gitignore, and nothing inside can be re-included. Treating logs/** as if it were logs/ is exactly the shortcut that loses keep.txt.

Checking against real git

Claims about gitignore are easy to get wrong, so the companion example uses real git as the oracle. It writes a small tree to a temporary directory, runs git init, and asks git status --porcelain --ignored -uall which files are untracked rather than ignored. With git 2.46.0, the tree from the pull request plus a few additions gives this:

.gitignore           logs/**   build/**   *.tmp
logs/.gitignore      !keep.txt
build/.gitignore     (a comment, no negations)

not ignored:  .gitignore   logs/keep.txt   src/app.py
ignored:      logs/.gitignore   logs/a.txt   logs/sub/keep.txt   build/.gitignore   build/out.bin   src/scratch.tmp

Two results are worth noticing. logs/.gitignore is itself ignored, because logs/** matches it, and git still reads it, because ignore files are read from every directory git enters. And logs/sub/keep.txt is ignored, which confirms the parent-directory rule.

One trap when checking by hand: git check-ignore -v logs/ reports a match for logs/**, which looks as if git excludes the directory. git status shows otherwise, because it lists logs/keep.txt as untracked. When the two disagree, trust the traversal.

Three walkers

The example implements three walkers over the same tree.

  • git_like models git's rules for the small pattern subset in the example: it enters a directory unless the directory itself is ignored, loads that directory's .gitignore, and applies the last matching rule.
  • prune_naive treats every dir/** rule as permission to skip dir entirely, the behaviour the pull request removed.
  • prune_guarded skips dir for a dir/** rule only if no negation could apply: none in the rules already loaded, and none in dir/.gitignore.
walker          kept                                       pruned           .gitignore files read
git_like        .gitignore  logs/keep.txt  src/app.py      logs/sub/        root, build, logs
prune_naive     .gitignore  src/app.py                     build/  logs/    root
prune_guarded   .gitignore  logs/keep.txt  src/app.py      build/  logs/sub/ root, logs

The naive walker loses logs/keep.txt and never reads logs/.gitignore. The guarded walker keeps it, agrees with git on every file, and still prunes build/ without reading build/.gitignore, which is the optimisation worth having.

The guard is short:

same_file_negation = any(neg for _, _, neg in rules)
nested = tree.get(f'{d}/.gitignore', '')
nested_negation = any(line.strip().startswith('!') for line in nested.splitlines())
return not (same_file_negation or nested_negation)

Why checking one level is enough

The guard reads only dir/.gitignore, not every .gitignore below dir. That is sufficient for dir/**, and the reason is the parent-directory rule. Every subdirectory of dir is matched by dir/** and therefore excluded, so git never enters it and never reads its .gitignore. A negation two levels down cannot re-include anything. Only the first level, where the directory itself is not excluded, can change the answer.

Negations in the same file matter too. With logs/** followed by !logs/keep.txt in the root .gitignore, git re-includes logs/keep.txt. Gemini CLI already handled that case before the pull request, by checking whether any negation in the loaded patterns could match inside the directory; the pull request added the nested-file check. The companion guard is deliberately cruder: any negation in the loaded rules blocks pruning. That is safe and prunes less than it could, and it is the right trade for a teaching model.

Why this matters for agents

An agent that cannot see a file behaves as if the file does not exist. In the example, keep.txt is exactly the kind of file a repository owner re-includes on purpose: a placeholder that keeps a directory in version control, a sample log that documents the format, a configuration file inside an otherwise generated directory. An agent asked to explain the log format, or to fix a bug in that configuration, would search, find nothing, and either say so or invent an answer.

There is also a safety side. Ignore rules often decide what an agent is allowed to read, and some teams add files to .gitignore to keep them away from tools. A walker that disagrees with git in either direction is a policy bug. Over-ignoring hides files the owner intended to be visible; under-ignoring would expose files the owner meant to keep out. Both deserve tests against real git.

Failure modes in your own walker

Patterns with wildcards in the directory part, such as packages/*/**, cannot be turned into a single directory prefix. Gemini CLI skips the expansion for them, and a homemade walker should too.

Escaped exclamation marks start a literal pattern, not a negation: \!important.txt ignores a file whose name begins with !. The pull request describes looking for "un-escaped" negations; the companion model does not support escapes at all.

Global excludes, .git/info/exclude and core.excludesFile also feed git's decision, and a walker that reads only .gitignore files can disagree with git on a developer's machine.

Case sensitivity differs: on a case-insensitive file system with core.ignorecase set, git matches patterns case-insensitively.

Verification

The companion example has six tests: the naive walker loses the re-included file and reads only the root .gitignore; the guarded walker matches the git model; the guarded walker still prunes where no negation applies; a negation cannot reach under an excluded directory; a same-file negation disables pruning; and the model agrees with real git on two trees, skipped if git is not on the path. Run python -m unittest -v. The pattern support is a small subset; for anything beyond it, ask git.

Run the example yourself

The three walkers, the synthetic tree, the real-git oracle and six tests. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.

Primary references and authorship

Sources read 23 November 2026. Original explanation, measurements and synthetic examples by Sandeep Belgavi. Views are my own and do not represent any employer, past or present.

Related on this site: