Order matters. Both rules match the same file, so the later one wins, and the file is ignored. Your earlier !
is overridden by your second rule.
You need to put the exclusion second:
www/*
!www/.gitignore
Question
I'm pretty new to Git and trying to understand how it works and why. I have a web project that I'm working on and I have a .gitignore
file that has the following line:
www/*
Now, I need to add just the www folder to my repo, so I've added just an empty .gitignore
file to it and updated my main .gitignore
file like so:
!www/.gitignore
www/*
Except, now when I do git status
, nothing comes up as needed to be added. In order to add it to the staging, I have to do git add www/.gitignore -f
.
Why doesn't Git just recognize the exclusion automatically and add the file for me when I do git add *
? Thanks!
Solution
Order matters. Both rules match the same file, so the later one wins, and the file is ignored. Your earlier !
is overridden by your second rule.
You need to put the exclusion second:
www/*
!www/.gitignore
OTHER TIPS
Try removing anything about the www
directory from your root .gitignore
, then make the contents of www/.gitignore
:
*
!.gitignore
This is how my repo is set up currently and I've had no issues.