22

我很惊讶 .gitignore 文件中的 *.user 之类的简单模式似乎与文件文件夹名称匹配。

ringods$ mkdir TestIgnore
ringods$ cd TestIgnore/
ringods$ git init
Initialized empty Git repository in /Users/ringods/Projects/hostbasket/TestIgnore/.git/
ringods$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
ringods$ mkdir security.user
ringods$ touch security.user/file_may_not_be_ignored.txt
ringods$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   security.user/
nothing added to commit but untracked files present (use "git add" to track)
ringods$ echo "*.user"> .gitignore
ringods$ cat .gitignore 
*.user
ringods$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
nothing added to commit but untracked files present (use "git add" to track)

我有错误的期望吗?如何编写带有扩展名 blah 的简单忽略文件并防止匹配以.blah结尾的文件夹?

gitignore手册页提到没有 / 的模式使用 shell glob 模式功能进行匹配,但它并没有真正告诉我它是仅匹配文件还是匹配文件和目录。

4

1 回答 1

49

.gitignore模式只匹配目录条目或路径。没有特定的方式可以说“仅匹配常规文件”,但是如果您提供尾随,/则该模式将仅匹配目录。您可以使用它来匹配具有两种模式的非目录(这几乎是您想要的):

*.user     # ignore all paths ending in '.user'
!*.user/   # but don't ignore these paths if they are directories.
于 2013-05-27T09:46:41.243 回答