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

Re: negated list in .gitignore no fun

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Dec 18, 2008, 22:38 UTC
Message-ID
<alpine.LFD.2.00.0812181429100.14014@localhost.localdomain>
In-Reply-To
<87hc51tajw.fsf@jidanni.org>
On Fri, 19 Dec 2008, jidanni@jidanni.org wrote:
Show 16 quoted lines
> 
> I had dreams of tracking only a few files in a large tree.
> I thought I would maintain that list as a negated list in .gitignore,
> and then always use "git-add ." to keep git's index reflecting my list.
> 
> However that's just not possible.
> 
> # head -n 5 .gitignore
> *
> !X11/xorg.conf
> !anacrontab
> !apt/apt.conf.d/10jidanni
> !apt/sources.list
> # git-add .
> But git-status only shows anacrontab got added. None of the files in
> the subdirectories get added.

Yeah, the problem is that the '*' matches the subdirectories (like "X11"), but the negative matching does not. So the subdirectory gets ignored, and as a result, git won't even traverse into it and notice that there's a file in there that shouldn't be ignored.

It's actually logical, but not what you want.
So you have several possibilities:
 (a) either create a .gitignore that looks like this:
	*
	!X11
	!X11/xorg.conf
	!anacrontab
	!apt
	!apt/apt.conf.d
	!apt/apt.conf.d/10jidanni
	!apt/sources.list
    which should work around it by telling git that it shouldn't ignore 
    the subdirectories.
 (b) realize that ".gitignore" only matters for files that git doesn't 
     already know about, so if you only want to track a small set of 
     files, what you _should_ do is just do a .gitignore that looks like 
     this:
	*
     and then just force-add the few files you want to track, using 
     something like
	git add -f X11/xorg.conf anacrontab apt/apt.conf.d/10jidanni apt/sources.list
     and now you're done - git won't ignore them, since explciitly you 
     told git to track them.
 (c) Try to teach git to not ignore subdirectories leading up to 
     non-ignored files, and give you the .gitignore semantics you like. I 
     suspect it's not worth it, because the git behaviour is logical once 
     you know about it and understand it.
.. and possibly other things you could do too.
			Linus
Previous: Boyd Stephen Smith Jr.Next: Linus Torvalds
Message 3 of 4 in “negated list in .gitignore no fun”
  1. jidanni@jidanni.orgDec 18, 2008
  2. Boyd Stephen Smith Jr.Dec 18, 2008
  3. Linus TorvaldsDec 18, 2008
  4. Linus TorvaldsDec 18, 2008

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.