.gitignore文件忽略目录中的文件的正确语法是什么?

会是吗

config/databases.yml
cache/*
log/*
data/sql/*
lib/filter/base/*
lib/form/base/*
lib/model/map/*
lib/model/om/*

or

/config/databases.yml
/cache/*
/log/*
/data/sql/*
/lib/filter/base/*
/lib/form/base/*
/lib/model/map/*
/lib/model/om/*

?


当前回答

前导斜杠表示忽略条目仅对.gitignore文件所在的目录有效。指定*。O将忽略该目录下的所有. O文件和所有子dirs,而/*. O将忽略该目录下的所有。O会忽略dir目录下的它们,而/foo/*。O只会忽略/foo/*. O。

其他回答

模式的格式

A blank line matches no files, so it can serve as a separator for readability. A line starting with # serves as a comment. An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources. If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git). If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file). Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, Documentation/*.html matches Documentation/git.html but not Documentation/ppc/ppc.html or tools/perf/Documentation/perf.html. A leading slash matches the beginning of the pathname. For example, /*.c matches cat-file.c but not mozilla-sha1/sha1.c.

你可以在这里找到更多

git help gitignore 或 男人吉特诺

第一个。这些文件路径与你的.gitignore文件的位置是相对的。

包含斜杠的路径相对于包含.gitignore文件的目录-通常是存储库的顶层,尽管您也可以将它们放在子目录中。

因此,由于在您给出的所有示例中,路径都包含斜杠,因此两个版本是相同的。唯一需要使用前导斜杠的时候是路径中还没有斜杠的时候。例如,要在存储库的顶层忽略foo,请使用/foo。简单地编写foo将忽略存储库中任何名为foo的东西。

通配符也是多余的。如果你想忽略整个目录,简单地命名它:

lib/model/om

使用通配符的唯一原因是如果你打算随后取消忽略目录中的某些内容:

lib/model/om/*      # ignore everything in the directory
!lib/model/om/foo   # except foo

我正在维护一个基于GUI和CLI的服务,允许您在https://www.gitignore.io上非常容易地生成.gitignore模板。

您可以在搜索字段中键入所需的模板,也可以安装命令行别名并运行

$gi 斯威夫特,奥斯汀

它将是:

config/databases.yml
cache
log
data/sql
lib/filter/base
lib/form/base
lib/model/map
lib/model/om

甚至可能是:

config/databases.yml
cache
log
data/sql
lib/*/base
lib/model/map
lib/model/om

如果过滤器和表单是lib中唯一需要忽略basesubdirectory的目录(可以将其视为可以使用星号的示例)。