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

Re: Feature Request: gitignore recursion

From
Jeff King <peff@peff.net>
Date
Oct 10, 2013, 16:22 UTC
Message-ID
<20131010162244.GA21489@sigill.intra.peff.net>
In-Reply-To
<52565E6A.2020907@gmail.com>
On Thu, Oct 10, 2013 at 09:59:38AM +0200, Karsten Blees wrote:
Show 22 quoted lines
> > On Mon, Oct 07, 2013 at 07:24:11AM -0400, Jeff King wrote:
> >> However, one thing I expected to work but didn't is:
> >> 
> >>   echo '*' >.gitignore
> >>   echo '!*' >my_dir/.gitignore
> >> 
> >> That _does_ work for attributes, like:
> >> 
> >>   echo '* foo=one' >.gitattributes
> >>   echo '* foo=two' >my_dir/.gitattributes
> >> 
> >> where the more-specific file takes precedence. It works because we keep
> >> an attribute stack, and look from most-specific directory to least.
> 
> Note that this doesn't work either:
> 
>   echo '*' >.gitignore
>   echo '!my_dir/*' >>.gitignore
> 
> The problem isn't that git doesn't read 'my_dir/.gitignore'. Git
> simply doesn't recurse into excluded directories, so patterns on
> excluded content have no effect.
Good point. To make it work you need to un-ignore the subdir, like:
  *
  !my_dir/

at which point "!my_dir/*" will work. But then so will a child .gitignore in subdir. In fact, I think the only reason that "!my_dir/**" is needed in the first place is that the "*" is too over-reaching; it ignores both the top-level directories _and_ all of the individual files in included directories.

So I think the best solution for the original problem, which is to ignore everything except for a particular directory, is:

  # ignore everything at the top-level...
  /*
  # ...but specifically include one directory
  !/my_dir/

and then we do not even need to use "**" matching at all (which was really about overriding the over-reaching "*").

> IMO this behavior is reasonable, as there is no way to check whether a
> negative pattern will match within an excluded untracked directory
> without scanning the directory.

Right. I was focused on not reading the .gitignore, but not descending into the ignore subdir affects patterns we already know about, too.

Show 7 quoted lines
> --- 8< ---
> Subject: [PATCH] gitignore.txt: clarify recursive nature of excluded directories
> 
> Additionally, precedence of negated patterns is exactly as outlined in
> the description, we don't need to repeat this.
> 
> Signed-off-by: Karsten Blees <blees@dcon.de>
Yeah, I think your update improves things.

I wonder if it is worth adding the "exclude everything except one directory" case above to the EXAMPLES section, too.

-Peff
Previous: Karsten BleesNext: Karsten Blees
Message 6 of 7 in “Feature Request: gitignore recursion”
  1. AJOct 6, 2013
  2. Duy NguyenOct 7, 2013
  3. Jeff KingOct 7, 2013
  4. Duy NguyenOct 7, 2013
  5. Karsten BleesOct 10, 2013
  6. Jeff KingOct 10, 2013
  7. Karsten BleesOct 10, 2013

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.