6

I have a directory called /static. There is a lot of subdirectories in it. I need to ignore all files in all subdirectories of the /static/ directory except of .htaccess, null.jpg and index.php files. Tell me please what is the right syntax for this operation?

/static/**
!/static/**/.htaccess

and

/static/*
!/static/*/.htaccess

don't work.

1
  • I think you should whitelist the directories first. Does !/static/**/ work as second line in your first approach? Commented May 27, 2015 at 3:32

1 Answer 1

10

As I mentioned in "Including specific file extension in gitignore", the main rule to remember is:

It is not possible to re-include a file if a parent directory of that file is excluded. (*)
(*: unless certain conditions are met in git 2.?+, see below)

That is why any rule which ignores folders (like * or */) would make excluding any sub-files impossible.

That is why the right approach is to exclude everything except:

  • folders,
  • (then) the files you want to exclude.

If you don't exclude folders first, your files would still be ignored (because of the rule I mention above)

So add in your .gitignore:

/static/**/**
!/static/**/
!.gitignore
!.htaccess

This is tested with Git 2.4.1 and works even on Windows.


Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

However, since one of the conditions was "The directory part in the re-include rules must be literal (i.e. no wildcards)", you cannot use that feature here anyway.

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