git_gitignore的配置(exclude&include)/只跟踪指定目录下的指定文件

文档

PATTERN FORMAT 部分:

   •   A blank line matches no files, so it can serve as a separator for
       readability.

   •   A line starting with # serves as a comment. Put a backslash ("\") in front of
       the first hash for patterns that begin with a hash.

   •   Trailing spaces are ignored unless they are quoted with backslash ("\").

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

   •   The slash / is used as the directory separator. Separators may occur at the
       beginning, middle or end of the .gitignore search pattern.

   •   If there is a separator at the beginning or middle (or both) of the pattern,
       then the pattern is relative to the directory level of the particular
       .gitignore file itself. Otherwise the pattern may also match at any level
       below the .gitignore level.

   •   If there is a separator at the end of the pattern then the pattern will only
       match directories, otherwise the pattern can match both files and
       directories.

   •   For example, a pattern doc/frotz/ matches doc/frotz directory, but not
       a/doc/frotz directory; however frotz/ matches frotz and a/frotz that is a
       directory (all paths are relative from the .gitignore file).

   •   An asterisk "*" matches anything except a slash. The character "?" matches
       any one character except "/". The range notation, e.g.  [a-zA-Z], can be used
       to match one of the characters in a range. See fnmatch(3) and the
       FNM_PATHNAME flag for a more detailed description.

匹配文件还是目录?

我们将.gitignore中的每一行(每一个规则)称为一个匹配模式(pattern)

如果某个匹配模式以/ (slash)结尾,那么该模式匹配的是目录(而不匹配文件(此外,它也不匹配符号连接!))
否则该模式既匹配目录,又匹配文件

The pattern doc/frotz and /doc/frotz have the same effect in any .gitignore file.
In other words, a leading slash is not relevant if there is already a middle slash in the pattern.

*

"*" matches anything except a slash.
The character “?” matches
any one character except “/”.

**

**:with infinite depth.

只跟踪指定目录下的指定文件

从文档中我们可以知道,某个目录如果被排除跟踪,那么其子目录将无法再次被强调跟踪
因此,如果您想要强调跟踪的文件(子目录)所在的目录不能够从目录的角度排除跟踪(以pathName的形式写入.gitignore中),而应该使用变通的方式pathName/*如此一来,排除的就不是目录,而是该目录下的所有子文件(子目录),这样其中的子项目可以被include again
(即,编写关于被完全排除的目录的子项的排除规则是不起作用的)

其他pattern也有类似的限制,所以,使用pathName/*这种形式往往会更加灵活.

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

   $ cat .gitignore
   #eclude everything except directory foo
   /*
   !/foo
   # for deeper specific path example:
   # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar

另一实例

仅排除指定目录test,但是要保留该目录中的.json文件
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

Guess you like

Origin blog.csdn.net/xuchaoxin1375/article/details/121196374