git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[Feature Request] Negated non-wildcard patterns in excluded directories in `.gitignore`

From
JKJoschua Kesper <joschua.kesper@tu-dortmund.de>
Date
Mar 4, 2025, 19:17 UTC
Message-ID
<43b26c9b-fd08-4a21-8371-9fd53f54694a@tu-dortmund.de>
Dear Git Community,

I have the somewhat unusual use-case and use negated patterns somewhat regularly. If I have a path `foo/bar/baz` which I want to track with git, but don't want any other files in `foo`. I would have to write the following in the `.gitignore`:

	foo/*
	!foo/bar
	foo/bar/*
	!foo/bar/baz
Instead of the simpler:
	foo
	!foo/bar/baz
In `gitignore(5)` you mention:
 > It is not possible to re-include a file if a parent directory of that
 > file is excluded. Git doesn’t list excluded directories for
 > performance reasons, so any patterns on contained files have no
 > effect, no matter where they are defined.

This is (I think) due to patterns like `!*.c`. So I propose changing it so exact-matching pattern parts like `foo` or `bar` to always work. This could be done by expanding it to the form mentioned above.

This change wouldn't affect performance I would argue, since writing `!foo/bar/baz` is pretty unambiguous in what the user intended. Depending on the implementation it may even improve performance, since we would only require listing of `foo/bar/baz` and not all of it's parents (though I am simply guessing the implementation for `<something>/*` here).

Joschua
Message 1 of 1 in “[Feature Request] Negated non-wildcard patterns in excluded directories in `.gitignore`”
  1. Joschua KesperMar 4, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.