I've done it. It seems that what is needed is a .gitignore
file inside the desired folder.
Look at this repository, which does exactly what you want.
I've tried to add any filename inside foo/
, foo/bar/
and foo/bar/baz/
but it only accepts .gitkeep.
The trick is to create a .gitignore with this content, inside the folder:
*
!*/
!.gitignore
!.gitkeep
[Edit: Added based on comments]
And you should be able to shorten this to:
*
!*/
!.git*
Though it may be less clear in the future what was intended.
While this solution doesn't shorten the number of lines required from your original .gitignore
, this does have several other advantages - the main one being that this will be a .gitignore
file inside the folder, which means it can be a clean file that has just these lines, and is specific to just this folder.
That means this .gitignore
file can be easily moved into any specific folder that requires this filter. Also, the entire folder can be easily moved around within the repo, or even moved to a different repo, without needing to modify the .gitignore
. It is much cleaner and more maintainable, and not buried in the typically large .gitignore
file in the repo's root folder.