如何gitignore *只*文件?

时间:2013-05-27 09:35:28

标签: git gitignore

我很惊讶.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 man page提到没有/的模式使用shell glob模式功能匹配,但它并没有真正告诉我它是仅匹配文件还是文件和目录。

1 个答案:

答案 0 :(得分:32)

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

*.user     # ignore all paths ending in '.user'
!*.user/   # but don't ignore these paths if they are directories.