echo "!*" > lib/.gitignore
is the simplest way to make this work.
What's going on is that !
means "don't ignore something that a previous match to this pattern would tell you to ignore" -- and /lib/
matches only directories. No previous pattern tells git to ignore that directory.
An alternate way to explain it that I think is clearer is: excluding a directory tells git "don't even look here", which is subtly different to matching every file within. Perhaps most clearly, if you had told it /lib/
, git would never check (never even see) the lib/.gitignore
above. When you said '!/lib/
' you told it to go ahead and look there, which it (would have anyway here, and) does.