否定模式在 .gitignore 中如何工作?

发布于 2024-09-01 06:53:29 字数 535 浏览 2 评论 0原文

我正在尝试使用带有否定模式的 .gitignore 文件(以 ! 开头的行),但它没有按我预期的方式工作。

作为一个最小的例子,我有以下目录结构:

C:/gittest
 -- .gitignore
 -- aaa/
   -- bbb/
     -- file.txt
   -- ccc/
     -- otherfile.txt

在我的 gitignore 文件中,我有这样的:

aaa/
!aaa/ccc/

我的理解(基于 ​​这个文档页面)是文件aaa/ccc/otherfile.txt不应该被忽略,但事实上git忽略了aaa下的所有内容。

我是否误解了这句话:“一个可选的前缀!它否定了该模式;任何被先前模式排除的匹配文件将再次包含在内。”?

顺便说一句,这是在 Windows 上使用 msysgit 1.7.0.2。

I am attempting to use a .gitignore file with negated patterns (lines starting with !), but it's not working the way I expect.

As a minimal example, I have the folllowing directory structure:

C:/gittest
 -- .gitignore
 -- aaa/
   -- bbb/
     -- file.txt
   -- ccc/
     -- otherfile.txt

and in my gitignore file, I have this:

aaa/
!aaa/ccc/

My understanding (based on this doc page) is that the file aaa/ccc/otherfile.txt should not be ignored, but in fact git is ignoring everything under aaa.

Am I misunderstanding this sentence: "An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again."?

BTW, this is on Windows with msysgit 1.7.0.2.

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

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

发布评论

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

评论(3

素衣风尘叹 2024-09-08 06:53:29

我认为你真正想做的是:

aaa/*
!aaa/ccc

你告诉它“不要查看aaa”,所以它甚至从不检查路径aaa/ccc。如果您使用通配符,它​​仍然读取 aaa 的内容,然后每个条目与通配符匹配并被忽略,除了 aaa/ccc 被放回。

I think that what you actually want to do is:

aaa/*
!aaa/ccc

You're telling it "don't look in aaa" so it never even examines the path aaa/ccc. If you use the wildcard, it still reads the contents of aaa, then each entry matches the wildcard and is ignored, except aaa/ccc which gets put back in.

真心难拥有 2024-09-08 06:53:29

如果您想排除 aaa 中的所有内容,但包含 aaa/ccc 及其下面的所有内容,则应该使用:

aaa/*
!aaa/ccc
!aaa/ccc/*

第一行告诉 git 忽略 aaa 下面的所有内容,第二行告诉它不要忽略文件夹 aaa/ccc,它实际上“启用”了第三行,然后告诉它不要忽略 aaa/ccc 下面的所有内容>。

If you want to exclude everything in aaa, but include aaa/ccc and everything beneath it, you should use:

aaa/*
!aaa/ccc
!aaa/ccc/*

The first line tells git to ignore everthing beneath aaa, the second tells it not to ignore the folder aaa/ccc which actually "enables" the third line which then tells it not to ignore everything beneath aaa/ccc.

一刻暧昧 2024-09-08 06:53:29

如果有人仍然没有在 git status 中看到新的未忽略的项目,那么事先运行 git update-index 可以帮助 git 查看更改(至少在版本 1.9 中)。 gitbash 的 x)。

If anyone's still not seeing newly un-ignored items in a git status running a git update-index before hand can help git to see the changes (at least in version 1.9.x of gitbash).

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