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

Odd .gitignore behaviour

From
BSBruce Stephens <bruce.stephens@isode.com>
Date
Nov 15, 2007, 12:49 UTC
Message-ID
<80ir431xny.fsf@tiny.isode.net>
Perhaps I'm misreading the manpage, but I think this is wrong:
    % mkdir base; cd base
    % git init
    % mkdir -p sub1/sub2
    % cd sub1
    % echo foo > .gitignore; echo '!sub2/foo' >> .gitignore
    % touch sub2/foo
    % git add sub2/foo
    The following paths are ignored by one of your .gitignore files:
    sub1/sub2/foo
    Use -f if you really want to add them.
    fatal: no files added

So sub1/sub2/foo matches the first pattern in sub1/.gitignore, but it also matches the negated pattern '!sub2/foo' (in the same file, so precedence isn't an issue). And the manpage says

       o An optional prefix ! which negates the pattern; any matching
           file excluded by a previous pattern will become included
           again. If a negated pattern matches, this will override
           lower precedence patterns sources.

So surely sub1/sub2/foo ought to be included again? Or is the first line in sub1/.gitignore not "a previous pattern" in this sense?

If I move the "foo" pattern up a level, creating a .gitignore in base just containing "foo", then sub1/sub2/foo is still regarded as ignored, even though it surely matches the negating pattern sub1/.gitignore, and that should be of higher precedence than the pattern in base/.gitignore?

Next: Linus Torvalds
Message 1 of 8 in “Odd .gitignore behaviour”
  1. Bruce StephensNov 15, 2007
  2. Linus TorvaldsNov 15, 2007
  3. Bruce StephensNov 15, 2007
  4. Junio C HamanoNov 15, 2007
  5. Junio C HamanoNov 15, 2007
  6. Fix per-directory exclude handing for "git add"Junio C Hamano, Nov 16, 2007
  7. Bruce StephensNov 16, 2007
  8. Bruce StephensNov 16, 2007

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.