gitignore匹配带有pre和suffix子目录的文件

时间:2012-01-13 11:56:20

标签: git gitignore

我喜欢只跟踪以“es”开头并以* .h或* .m

结尾的文件

所以我试过这个:

#exlude all
*
#except
!es*.h
!es*.m

#exlude all
*
#except
!*/es*.h
!*/es*.m

但不适用于子目录中的文件

1 个答案:

答案 0 :(得分:2)

当您忽略所有内容(*)时,您忽略文件夹,即使它们也有内容,因为*匹配所有内容。

如果您 unignore 某些内容,那么只会匹配根目录的文件。如果您需要取消签名目录,则需要明确说明(即!mydir/)。但这会忽略该目录的所有内容,因此您必须重新定义该目录内容的忽略/ unignore模式。即使你这样做,如果你还没有将dir添加到索引中,你也不会在git status中看到它。

您的案例可以轻松解决,但可以反转模式 你基本上想要做的是忽略所有

  • 不以es
  • 开头
  • 不会以.h .m结尾。

这样做:

$ ls -a
.  ..  .git  .gitignore  a  b  blah  c  ebar.m  esfoo.h  esfoo.m  sbar.m
$ ls -a blah/
.  ..  a  b  c  ebar.m  esfoo.h  esfoo.m  sbar.m
$ git status -s
?? blah/
?? esfoo.h
?? esfoo.m
$ git status -s blah/   # matching files ignored and also ignored on `git add`
?? blah/esfoo.h
?? blah/esfoo.m
$ git add .
$ git status -s         # only wanted files were added
A  blah/esfoo.h
A  blah/esfoo.m
A  esfoo.h
A  esfoo.m
$ cat .gitignore        # the ignore pattern -- ignore
[^e]*                   # everything that doesn't start with 'e'
e[^s]*                  # and is not followed by an 's'
*.[^hm]                 # and does not end with '.h' or '.m'
!/blah                  # uningore the wanted subdirs

正如您在上一个命令中看到的那样,我已将您的模式反转为忽略所有不以e开头并且后面没有s并且不以{结尾{1}}或.h并且还没有签署一个目录。即使dir有更多的内容,它也会被忽略,因为它与模式相匹配,只添加了想要的部分。

编辑:已更新