You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何.gitattributes中反向export-ignore规则不生效?是否为Bug?

问题

当在.gitattributes文件中配置如下反向export-ignore规则时:

* export-ignore

composer.json              -export-ignore
bin/lean-package-validator -export-ignore
resources/                 -export-ignore
src/                       -export-ignore

执行git archive -o test-archive.zip HEAD命令创建归档,该规则并未生效。唯有使用如下规则,才能生成预期的发布产物文件:

* export-ignore

composer.json              -export-ignore
bin/                       -export-ignore
bin/lean-package-validator -export-ignore
resources/                 -export-ignore
resources/**               -export-ignore
src/                       -export-ignore
src/**                     -export-ignore

请问这是否属于Git的Bug?


解答

这不是Git的Bug,而是.gitattributes路径匹配规则的正常行为,核心逻辑如下:

  1. *通配符的匹配范围限制
    初始规则里的* export-ignore会匹配仓库内所有文件和目录,但这个规则不会自动递归覆盖子目录内容。给resources/或src/添加-export-ignore时,仅排除了目录本身被忽略,但目录下的文件依然会被* export-ignore命中,导致它们被排除在归档外。

  2. 递归匹配需要明确声明
    Git的.gitattributes规则默认不具备递归性,若要让目录下所有内容都跳过export-ignore,必须用**通配符明确匹配目录及其下所有层级的文件/子目录。添加resources/** -export-ignore和src/** -export-ignore后,整个目录的内容才会被包含进归档。

  3. 单个文件与目录的规则差异
    对于bin/lean-package-validator这类单个文件,直接添加-export-ignore即可生效;但目录本身的规则不会覆盖内部文件的匹配结果,必须单独声明递归规则。

简化后的有效规则

无需同时声明目录本身和递归规则,直接用**匹配整个目录树即可:

* export-ignore

composer.json              -export-ignore
bin/lean-package-validator -export-ignore
resources/**               -export-ignore
src/**                     -export-ignore

如果想包含整个bin目录,也可以用bin/** -export-ignore替代单个文件的规则。

内容的提问来源于stack exchange,提问作者raphaelstolt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.02 02:54:51