[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.

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.

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:
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?

2 Answers 2


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.

  • 2
    Actually, the second answer there is needlessly complex; the first answer was all I needed. All I had to do was change line 2 of my pasted .gitignore above to sites/*/files/*, and everything works great now. Thanks! Aug 24, 2012 at 20:46
  • thank you for this comment @75thTrombone Sep 24, 2020 at 7:34

Google brought me here in search for an answer for a similar problem. All I wanted to do is keep track of a single configuration file in an otherwise un-modified library.

In order accomplish this I had to include/exclude every folder up until the file I wanted to exclude:

vendor/* !vendor/tfox/ vendor/tfox/* !vendor/tfox/mpdf-port-bundle/ vendor/tfox/mpdf-port-bundle/* !vendor/tfox/mpdf-port-bundle/TFox/ vendor/tfox/mpdf-port-bundle/TFox/* !vendor/tfox/mpdf-port-bundle/TFox/MpdfPortBundle/ vendor/tfox/mpdf-port-bundle/TFox/MpdfPortBundle/* !vendor/tfox/mpdf-port-bundle/TFox/MpdfPortBundle/mpdf/ vendor/tfox/mpdf-port-bundle/TFox/MpdfPortBundle/mpdf/* !vendor/tfox/mpdf-port-bundle/TFox/MpdfPortBundle/mpdf/config_fonts.php

This set of rules looks very bulky, but it seems to be the correct way to do this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.