git - .gitignore 文件中的/media 和 media/有什么区别?

标签 git gitignore

我都试过了,得到的结果相似。一直找不到答案。

最佳答案

开头和结尾的斜杠分别对匹配的内容添加了限制。

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

对于 media/:这将匹配 .gitignore 文件所在目录或任何子目录中名为“media”的任何目录/文件夹。它不会匹配名为“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 文件所在目录的所有文件路径,media 开始>。它不会匹配恰好包含“media”但在当前目录中不是以“media”开头的子目录中的路径。例如,“media/foo.c”会匹配,但“foo/media/bar.c”不会。

关于git - .gitignore 文件中的/media 和 media/有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47605405/

相关文章:

git - bitbucket.org : Permission denied (publickey)

git - 如何在 github 提交中设置用户名别名?

ruby - 为什么 git 在 vendors 目录中查找?

git - 文件不断返回

git - 删除/stash git 分支而不删除提交历史

Git 推送 "error: index-pack died of signal 9"

git - 尝试批量推送大量提交

gitignore - .gitignore 文件中的路径有何不同?

git - 从 'git pull' 中排除特定文件

git - 我如何告诉 Git 忽略除子目录之外的所有内容?