Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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! –  75th Trombone Aug 24 '12 at 20:46

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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