Skip to content

gitattributes:无效的模式或属性

快速摘要

你的 .gitattributes 文件有一行模式格式错误或属性名拼写错误 —— git 会警告或忽略该条目。

为什么发生

  • glob 模式使用了不支持的语法,或括号未闭合,git 无法将其解析为路径规范

  • 属性名拼写错误,如 'text=eol' 而非 'text eol=lf' 的正确语法

  • 某行字段数量错误 —— 有模式但无属性,或有属性但无模式

最小示例

✗ 错误代码
# .gitattributes: *.txt text=eol  (invalid syntax)
echo '*.txt text=eol' >> .gitattributes
✓ 修复代码
# .gitattributes: correct syntax
echo '*.txt text eol=lf' >> .gitattributes

text 和 eol 属性必须用空格分隔,eol 用 = 赋值 —— 用单个 = 合并它们是无效语法。

如何诊断

  • 运行 git check-attr -a <file> 查看实际应用的属性,检测缺失的属性

  • 检查每行 .gitattributes 的语法是否正确:pattern attr1 attr2=value

  • 使用 git check-attr <attribute> <file> 验证单个属性是否被 git 识别

如何修复

  • 将 glob 模式修正为有效的 gitignore 风格语法,括号和通配符需闭合

  • 修正属性名和值以匹配 git 识别的属性(text、binary、eol、filter、diff、merge 等)

  • 用 # 开头注释掉或删除无效行,防止 git 解析它们

如何预防

  • 提交 .gitattributes 修改前用 git check-attr 测试,验证属性正确应用

  • 参考 gitattributes 文档了解已识别的内置属性及其有效值格式

相关资源

相关术语

相关课程

← 返回语言