gitignore与子目录中的前缀和后缀匹配文件 [英] gitignore match files with pre and suffix in subdirectory

查看:835
本文介绍了gitignore与子目录中的前缀和后缀匹配文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

结尾的文件,所以我试过这个:

  #exlude全部
*
#except
!es * .h
!es *。 m

  #exlude全部
*
#except
!* / es * .h
!* / es * .m

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

解决方案

如果忽略所有内容( * ),即使它们也包含内容,即可忽略文件夹,因为 * 匹配一切。



如果您 unignore ,那只会匹配根目录的文件。如果你需要取消管理一个目录,你需要明确地说(即!mydir / )。但是,这将取消该目录中的所有内容,因此您必须重新定义该目录内容的忽略/统一格式。即使你这样做了,如果你还没有添加目录到索引,你将不会在 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 /#匹配文件被忽略,并在`git add`上被忽略
?? blah / esfoo.h
?? blah / esfoo.m
$ git add。
$ git status -s#只需要的文件被添加
一个blah / esfoo.h
一个blah / esfoo.m
一个esfoo.h
一个esfoo。 m
$ cat .gitignore#忽略模式 - 忽略
[^ e] *#所有不以'e'开头的
e [^ s] *#并且没有被遵守由's'
*。[^ hm]#并且不以'.h'或'.m'结尾
!/ blah#uningore想要的子目录


$ b

正如你在上一个命令中看到的那样,我已经颠倒了你的模式来忽略所有不以开头的东西, e ,并且后面跟着 s ,并且不以 .h 结尾。或者 .m ,同时也取消了一个dir。尽管dir有更多的内容,但它被忽略,因为它与模式匹配,只添加了所需的部分。

I like to track only files that start with "es" and end with *.h or *.m

so i tried this:

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

and

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

but neither works for files in subdirectories

解决方案

When you ignore everything (*), you ignore folders, even if they have contents too, as * matches everything.

If you unignore something, that would be matched only for the files of the root dir. If you need to unignore a directory, you need to explicitly say that (ie !mydir/). But that would unignore all the contents of that dir, so you'd have to redefine the ignore/unignore pattern for the contents of that dir. Even if you do that, if you haven't already added the dir to the index, you won't see that in git status.

Your case can be solved easily but inverting the pattern.
What you basically want to do is ignore everything that

  • does not start with es and
  • does not end with .h or .m.

So do that:

$ 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

As you see in the last command, I've inverted your pattern to ignore everything that doesn't start with e and is not followed by an s and that doesn't end with .h or .m and also unignored a dir. Even though the dir had more content, it was ignored as it matched the pattern, and only the wanted parts were added.

edit: updated

这篇关于gitignore与子目录中的前缀和后缀匹配文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆