.gitignore 文件中的 /media 和 media/ 有什么区别?
What's the difference between /media and media/ in a .gitignore file?
我都试过了,得到的结果相似。一直找不到答案。
默认情况下,gitignore 模式是递归的,即它将忽略包含 .gitignore
文件的子文件夹中与该模式匹配的文件或文件夹。
通过在模式前加上 /
前缀,您可以禁用递归性,并且模式仅适用于包含 .gitignore
文件的文件夹内的文件或文件夹。
通过在模式后缀 /
,您只匹配文件夹(而不是文件)。
如果将两者结合使用,则只会匹配当前文件夹中的文件夹,不会匹配子文件夹。
因此,如果当前目录中只有一个 media
文件夹,则两种模式都将匹配它,结果将相同。
开头和结尾的斜杠分别对匹配的内容添加了限制。
media
: 不带任何一个斜线,匹配当前目录下所有子目录下的文件和目录。
media/
:尾部加斜杠表示只匹配当前目录下的目录和所有子目录。 (不是文件)
/media
:带前导斜杠,它将匹配文件和目录,但只匹配当前目录。 (不是子目录)
/media/
:前导和尾部都带有斜杠,它将只匹配当前目录中的目录"media"。 (不是文件也不是子目录)
.gitignore documentation 在这些方面相当清楚:
使用尾部 /
指定目录或文件夹
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).
For media/
:这将匹配 .gitignore 文件所在目录中任何名为 "media" 的 directory/folders,或任何子目录。它不会匹配名为 "media" 的文件。例如,"media/foo.c" 和 "bar/media/foo.c" 会匹配,但文件 "foobar/media" 不会匹配。
使用前导/
指定匹配必须从当前目录开始
A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
对于/media
:
/media
将匹配 .gitignore 文件所在目录中的所有文件路径,其中 begin with media
.它不会匹配恰好包含 "media" 但在当前目录中不以 "media" 开头的子目录中的路径。例如,"media/foo.c" 会匹配,但 "foo/media/bar.c" 不会。
我都试过了,得到的结果相似。一直找不到答案。
默认情况下,gitignore 模式是递归的,即它将忽略包含 .gitignore
文件的子文件夹中与该模式匹配的文件或文件夹。
通过在模式前加上 /
前缀,您可以禁用递归性,并且模式仅适用于包含 .gitignore
文件的文件夹内的文件或文件夹。
通过在模式后缀 /
,您只匹配文件夹(而不是文件)。
如果将两者结合使用,则只会匹配当前文件夹中的文件夹,不会匹配子文件夹。
因此,如果当前目录中只有一个 media
文件夹,则两种模式都将匹配它,结果将相同。
开头和结尾的斜杠分别对匹配的内容添加了限制。
media
: 不带任何一个斜线,匹配当前目录下所有子目录下的文件和目录。media/
:尾部加斜杠表示只匹配当前目录下的目录和所有子目录。 (不是文件)/media
:带前导斜杠,它将匹配文件和目录,但只匹配当前目录。 (不是子目录)/media/
:前导和尾部都带有斜杠,它将只匹配当前目录中的目录"media"。 (不是文件也不是子目录)
.gitignore documentation 在这些方面相当清楚:
使用尾部 /
指定目录或文件夹
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 directoryfoo
and paths underneath it, but will not match a regular file or a symbolic linkfoo
(this is consistent with the way how pathspec works in general in Git).
For media/
:这将匹配 .gitignore 文件所在目录中任何名为 "media" 的 directory/folders,或任何子目录。它不会匹配名为 "media" 的文件。例如,"media/foo.c" 和 "bar/media/foo.c" 会匹配,但文件 "foobar/media" 不会匹配。
使用前导/
指定匹配必须从当前目录开始
A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
对于/media
:
/media
将匹配 .gitignore 文件所在目录中的所有文件路径,其中 begin with media
.它不会匹配恰好包含 "media" 但在当前目录中不以 "media" 开头的子目录中的路径。例如,"media/foo.c" 会匹配,但 "foo/media/bar.c" 不会。