With git version 1.7.1, I'm trying to exclude all files except .php files.
The working solution I found relies on the command !*/
# Ignore Everything
*
# Except these files
!.gitignore
!*/
!*.php
Without the !*/
, it will only include the *.php
files in the root directory. What is !*/
doing that allows this to work?
Take a look at the documentation of gitignore
An optional prefix "!" which negates the pattern; any matching file
excluded by a previous pattern will become included again. It is not
possible to re-include a file if a parent directory of that file is
excluded. Git doesn’t list excluded directories for performance
reasons, so any patterns on contained files have no effect, no matter
where they are defined. Put a backslash ("\") in front of the first
"!" for patterns that begin with a literal "!", for example,
"!important!.txt".
Here's my line of thinking:
The ignore statement *
will ignore everything by default, including the root directory and all of its contents.
So at this point, all files and folders in the root directory are ignored.
The !*.php
command will re-include all the *.php
files in the root path, but the folders are still being ignored (because they don't end in .php
) - and therefore, they're not negated from the gitignore yet.
So the !*/
command re-includes all the directories (and subsequent sub-directories) so that they can be examined for *.php
files. Example: folder1/
matches the negation statement !*/
because it contains the /
at the end and the folder name fits in the wildcard operator *