.gitignore规则之间带有/不带有反斜杠(如/dir和/dir/)的区别 [英] Difference between .gitignore rules with and without trailing slash like /dir and /dir/
问题描述
Git存储库中的.gitignore文件中的/dir
和/dir/
之间是否存在差异?
Is there a difference between /dir
and /dir/
in the .gitignore file within a Git repository?
以下内容有何不同?
/dir
/dir/
/dir/*
推荐答案
这是一个老问题,但在Google上排名最高,而投票最高的答案是错误.正确的答案在这里.
This is a old question, but high ranked on Google and the top voted answer is wrong. Here goes the correct answer.
是的,这些规则是不同的.
Yes, these rules are different.
-
/dir
将匹配文件,目录,链接以及任何名为dir
的内容
-
/dir/
将仅匹配 名为dir
的目录
-
/dir/*
将匹配所有文件,目录以及其中的任何其他内容 名为dir
的目录(而不是dir
目录本身).
/dir
will match a file, directory, link, anything nameddir
/dir/
will match only a directory nameddir
/dir/*
will match all files, directories and anything else inside a directory nameddir
(but not thedir
directory itself).
/dir
,/dir/
和/dir/*
等价于 NOT .区别
使用覆盖规则时非常清楚,例如著名的!.gitkeep
避开跟踪空目录的限制.认为
文件dir/.gitkeep
/dir
, /dir/
and /dir/*
are NOT equivalent. The difference
is very clear when using overriding rules, like the famous !.gitkeep
to get around the limitation of tracking empty directories. Suppose
the existence of the file dir/.gitkeep
- 使用
/dir
和/dir/
,Git甚至不会查看目录内部 这样就不会显示.gitkeep
. - 使用
/dir/*
,Git和目录将检测到该文件 如果提交此.gitkeep
,则将保留,因为该规则 不适用于目录本身,仅适用于目录内容.
- With
/dir
and/dir/
, Git won't even look inside the directory so the.gitkeep
won't be seen. - With
/dir/*
, the file will be detected by Git and the directory will be kept if this.gitkeep
is committed, because the rule doesn't apply to the directory itself, only to its contents.
观察:上面提到的所有规则都锚定在当前目录中
(.gitignore
所在的位置),因为/
前缀.
如果没有前缀,则规则不仅适用于该特定目录,还适用于子目录或存储库中的所有位置,
如果.gitignore
位于根级别.
OBSERVATION: All the rules mentioned above are anchored at the current directory
(the place where the .gitignore
is), because of the /
prefix.
Without the prefix, the rules would apply not only for that specific directory, but also for the sub-directories or everywhere in the repository,
if the .gitignore
is located at the root level.
这篇关于.gitignore规则之间带有/不带有反斜杠(如/dir和/dir/)的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!