What does !*/ mean in .gitignore

2019-04-19 18:25发布

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?

标签: git gitignore
2条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-19 18:54

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".

查看更多
做自己的国王
3楼-- · 2019-04-19 18:57

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 *

查看更多
登录 后发表回答