gitignore 匹配子目录中带有前缀和后缀的文件

发布于 2024-12-27 05:25:21 字数 220 浏览 2 评论 0原文

我喜欢只跟踪以“es”开头并以 *.h 或 *.m 结尾的文件,

所以我尝试了以下方法:

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

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

两者都不适用于子目录中的文件

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2025-01-03 05:25:21

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

如果您忽略某些内容,则该内容将仅与根目录的文件匹配。如果您需要取消忽略某个目录,则需要明确说明(即 !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/   # 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 的内容,并且不以 .h.m 结尾,并且也未忽略目录。即使目录有更多内容,它也会被忽略,因为它与模式匹配,并且只添加了想要的部分。

编辑:已更新

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文