Git doesn’t observe new exceptions in .gitignore

gitversion control

[NOTE: This is not the usual "Git still tracks files I just added to .gitignore!" problem. This is the opposite of that.]

I have a website's full directory structure in a Git repository. The CMS I use comes by default with a .gitignore file:

# Ignore paths that contain user-generated content.
sites/*/files/
sites/*/private/

Note that all sites/*/files directories are excluded. But the way I do my sites, there's a particular subfolder that I want to be included in the repo. So after starting my repo with the above .gitignore, I amended it to this:

# Ignore paths that contain user-generated content.
sites/*/files/
!sites/*/files/content/
!sites/*/files/content/*
sites/*/private/

By my understanding, the negated patterns I added above should mean that sites/*/files/content/ and its contents ARE included in the repo, but the rest of sites/*/files is ignored. And when I remember to update my .gitignore file before doing my first commit, it works as planned.

But when I forget to update the .gitignore until later, as I am wont to do, I can't get Git to add the new files without forcing it to ignore the .gitignore file completely. I run this:

> git add sites/default/files/content/

And get this back:

The following paths are ignored by one of your .gitignore files:
sites/default/files
Use -f if you really want to add them.
fatal: no files added

Why yes, Git, that directory IS ignored on line 2 of the .gitignore, but lines 3 and 4 say to make an exception for the specific folder I asked you about!

I'm aware that I can use -f to add it directly, but then I have to do it again and again on any future commit that adds files to that directory.

There are pages all over the web that talk about how updating .gitignore doesn't make Git ignore already-tracked files, but I can't find anything that talks about how it doesn't make Git track newly-excepted files.

Lots of places say that git update-index can be used to solve problems like this, but I can't seem to make it work for me. It has about a million possible flags, so maybe I didn't stumble upon the right one (or combination of several).

TL;DR: How can I make Git observe new exclusions in my .gitignore file?

Best Answer

I think this answer explains it best. Negation only works to re-include files that have been explicitly ignored by another rule. When you ignore an enclosing directory, Git doesn't even descend into it and hence never sees, nor explicitly excludes, your file.

The answer below the one above gives you the solution: You have to create one or more rules which build a set of paths that explicitly excludes everything you want to exclude including your file at which point you can negate the exclusuion for it or any of it's containing directories.

Related Question