You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
go-git ignores files inside un-ignored directory with wildcard pattern #2112
When using a .gitignore file with a wildcard ignore * followed by a directory-only inclusion like !dir/, go-git fails to ignore files inside dir/, whereas standard Git ignores them.
In plumbing/format/gitignore/pattern.go, the simpleNameMatch function iterates over all components of a path. If any component matches the pattern, it returns true (matched), unless it's the last component and it's checking dirOnly.
For a path ["dir", "file.txt"] and pattern !dir/:
Component dir matches dir.
It is not the last component (i == 0, len(path)-1 == 1).
The function returns true (matched).
This causes the file to be included, overriding the * pattern.
Effectively, !dir/ acts as !dir/** in go-git.
(I did use AI to generate a bug report with identifiers that avoid the non-public parts of my project, I edited it myself and ran the reproducer myself.)
Opened #2310 to fix this — simpleNameMatch was letting a dirOnly re-include pattern (!dir/) matching an ancestor path component stand in as a match for the full descendant path, so files nested under the re-included directory were wrongly reported as untracked instead of ignored. The PR description has "Fixes #2112" so this will auto-close once it's merged.
Update: the fix now lives in #2311 (replaces #2310, which had a commit-message/DCO CI failure — same change, just re-committed correctly). #2311 has the full write-up of the root cause and fix; short version: simpleNameMatch was letting a dirOnly re-include pattern (!dir/) that matched an ancestor path component stand in as a match for the entire descendant path, so files nested under the re-included directory were wrongly reported as untracked instead of ignored. "Fixes #2112" is in the PR description, so it'll auto-close this issue once merged.
I keep an outside conformance bench against git check-ignore, and this bug is the whole of what it finds in go-git's matcher: on today's main (0f3a0a2c), all 12 divergences over 8,953 queries are this one rule. Two open PRs offer to fix it — #2195 and #2311 — so I measured both against their own merge bases. Behaviour, not a code review; oracle is git 2.55.0.
A second code path, which neither PR's write-up mentions. The analysis here is about simpleNameMatch and a dirOnly re-include. globMatch cascades the same way, with an anchored multi-segment negation and no dirOnly anywhere:
Matcher.Match([]string{"a", "b", "c"}, false) returns false on main, and Status() lists a/b/c as untracked. !a/b re-includes the directory; git then re-tests what is inside it, and a/**/* still ignores that.
The pattern pairs are real: supabase/supabase ships volumes/functions/** + !volumes/functions/deno.json* and **/*/generated/**/* + !**/*/generated/.gitkeep, ollama/ollama ships .vscode/* + !.vscode/extensions.json. It bites when the re-included name is a directory on disk (when it is a file, go-git agrees) — which is also why the four rows left in #2386's Status() column after it fixes #2369 are this shape.
The two PRs. One Go helper (ReadPatterns + Matcher.Match, and Worktree.Status() as a second entry point), built once per column through a replace to a worktree of that commit, so only the library changes between rows. Corpus: 33 repositories' whole .gitignore trees materialised on disk, 8,953 matcher queries and 6,714 answered through Status(). Sets, not counts.
The three bases are 147 commits apart and give identical rows, same paths; that is what makes the deltas readable. (#2195's merge ref is computed against a base that predates Scope, so its column describes the patch on that base.)
What they buy.#2195 fixes the globMatch half — the repro above and the supabase rows both come right. #2311 fixes this issue's case verbatim — * + !my-dir/, my-dir/sub/file.txt ignored, checked by hand against both columns. No row of my corpus moves in its favour, which says the corpus doesn't sample that combination, not that the patch does nothing.
What they cost, and it is the same family both times — a directory excluded by one rule and re-included by a later one, where nothing else in the file matches the contents:
$ printf'core\n!core/\n'> .gitignore # python/cpython ships this, with a comment saying why
# a/core/keep git: not ignored main: not ignored +2311: ignored +2195: ignored
$ printf'.vscode\n!packages/playground/.vscode\n'> .gitignore # facebook/react, compiler/
# packages/playground/.vscode/settings.json
# git: not ignored main: not ignored +2311: ignored through Status() +2195: ignored
All 82 new rows under #2195 go that one way — git does not ignore, go-git does — across ten repositories: !.env.example (langchain, gradio), !.* and !deps/**/.* (node), !.vscode/settings.json (angular), !core/ (cpython), !.yarn/patches (next.js, tailwind), plus scikit-learn, vscode and react. #2311's 16 and 18 are the same shape, confined to cpython, react and vscode.
Both patches encode "a negation counts only if it matches the whole path". git's rule reads differently from here: the negation re-includes the directory, and everything inside is then tested again against every pattern. In this issue's case the contents stay ignored because * matches sub and file.txt on their own, not because !my-dir/ stopped counting; with core + !core/ nothing is left to ignore a/core/keep, which is why main is right there and both patches are not.
Activity
thesahibnanda-max commentedon Aug 11, 2026
Opened #2310 to fix this —
simpleNameMatchwas letting adirOnlyre-include pattern (!dir/) matching an ancestor path component stand in as a match for the full descendant path, so files nested under the re-included directory were wrongly reported as untracked instead of ignored. The PR description has "Fixes #2112" so this will auto-close once it's merged.plumbing: gitignore, dirOnly re-include patterns must not cascade to …
thesahibnanda-max commentedon Aug 11, 2026
Update: the fix now lives in #2311 (replaces #2310, which had a commit-message/DCO CI failure — same change, just re-committed correctly). #2311 has the full write-up of the root cause and fix; short version:
simpleNameMatchwas letting adirOnlyre-include pattern (!dir/) that matched an ancestor path component stand in as a match for the entire descendant path, so files nested under the re-included directory were wrongly reported as untracked instead of ignored. "Fixes #2112" is in the PR description, so it'll auto-close this issue once merged.KaizenShogun commentedon Sep 18, 2026
I keep an outside conformance bench against
git check-ignore, and this bug is the whole of what it finds in go-git's matcher: on today'smain(0f3a0a2c), all 12 divergences over 8,953 queries are this one rule. Two open PRs offer to fix it — #2195 and #2311 — so I measured both against their own merge bases. Behaviour, not a code review; oracle is git 2.55.0.A second code path, which neither PR's write-up mentions. The analysis here is about
simpleNameMatchand adirOnlyre-include.globMatchcascades the same way, with an anchored multi-segment negation and nodirOnlyanywhere:Matcher.Match([]string{"a", "b", "c"}, false)returnsfalseonmain, andStatus()listsa/b/cas untracked.!a/bre-includes the directory; git then re-tests what is inside it, anda/**/*still ignores that.The pattern pairs are real:
supabase/supabaseshipsvolumes/functions/**+!volumes/functions/deno.json*and**/*/generated/**/*+!**/*/generated/.gitkeep,ollama/ollamaships.vscode/*+!.vscode/extensions.json. It bites when the re-included name is a directory on disk (when it is a file, go-git agrees) — which is also why the four rows left in #2386'sStatus()column after it fixes #2369 are this shape.The two PRs. One Go helper (
ReadPatterns+Matcher.Match, andWorktree.Status()as a second entry point), built once per column through areplaceto a worktree of that commit, so only the library changes between rows. Corpus: 33 repositories' whole.gitignoretrees materialised on disk, 8,953 matcher queries and 6,714 answered throughStatus(). Sets, not counts.Status()main0f3a0a2c3cb46ce7refs/pull/2311/merged6cbbfaarefs/pull/2195/mergeThe three bases are 147 commits apart and give identical rows, same paths; that is what makes the deltas readable. (#2195's merge ref is computed against a base that predates
Scope, so its column describes the patch on that base.)What they buy. #2195 fixes the
globMatchhalf — the repro above and the supabase rows both come right. #2311 fixes this issue's case verbatim —*+!my-dir/,my-dir/sub/file.txtignored, checked by hand against both columns. No row of my corpus moves in its favour, which says the corpus doesn't sample that combination, not that the patch does nothing.What they cost, and it is the same family both times — a directory excluded by one rule and re-included by a later one, where nothing else in the file matches the contents:
All 82 new rows under #2195 go that one way — git does not ignore, go-git does — across ten repositories:
!.env.example(langchain, gradio),!.*and!deps/**/.*(node),!.vscode/settings.json(angular),!core/(cpython),!.yarn/patches(next.js, tailwind), plus scikit-learn, vscode and react. #2311's 16 and 18 are the same shape, confined to cpython, react and vscode.Both patches encode "a negation counts only if it matches the whole path". git's rule reads differently from here: the negation re-includes the directory, and everything inside is then tested again against every pattern. In this issue's case the contents stay ignored because
*matchessubandfile.txton their own, not because!my-dir/stopped counting; withcore+!core/nothing is left to ignorea/core/keep, which is whymainis right there and both patches are not.Bench, adapter and corpus: https://github.com/KaizenShogun/gitignore-conformance — every row above is a
gic.pyrun with--entry patternsor--entry status.