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

Re: Odd .gitignore behaviour

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Nov 15, 2007, 18:56 UTC
Message-ID
<alpine.LFD.0.9999.0711151044150.2786@woody.linux-foundation.org>
In-Reply-To
<80ir431xny.fsf@tiny.isode.net>
On Thu, 15 Nov 2007, Bruce Stephens wrote:
> 
> 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?
Yes.

The negated patterns have to show up *before* the patterns that they override, because the first pattern that matches is the one that is used.

Show 5 quoted lines
> 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?

The priority between nested .gitignore files should be that inner files have higher priority than outer files (since the inner ones "know more", and the outer ones are "generic").

So what you describe sounds wrong.

But my quick test didn't actually support the behaviour you see. In this situation:

	[torvalds@woody git-test]$ cat .gitignore
	a*
	[torvalds@woody git-test]$ cat subdir/.gitignore
	!a-ok
	[torvalds@woody git-test]$ find *
	all-files
	subdir
	subdir/a-ok
	[torvalds@woody git-test]$ git ls-files -o --exclude-per-directory=.gitignore
	.gitignore
	subdir/.gitignore
	subdir/a-ok

ie we *do* show "showdir/a-ok" (but we don't show "all-files") because a-ok is explicitly marked to be not ignored by a higher-priority rule.

Side note: "git status" uses the "--directory" flag, so it will not even recurse into unknown directories, and will instead apply the gitignore rules to the directory names, so you get:

	[torvalds@woody git-test]$ git ls-files -o --exclude-per-directory=.gitignore --directory
	.gitignore
	subdir/

which doesn't show "a-ok", but that's for a totally unrelated reason and has nothing to do with .gitignore!

		Linus
Previous: Bruce StephensNext: Bruce Stephens
Message 2 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.