Git Attributes
一句话解释
.gitattributes 文件为路径赋予属性,控制行尾、diff、合并与导出等按文件行为。
用大白话说
.gitattributes 文件让你为仓库中特定路径或模式赋予属性,控制 Git 如何对待它们。它是全局 git config 的仓库级伙伴,并提交到仓库,使所有贡献者共享相同规则。常见用途:强制行尾(* text=auto 或 *.bat text eol=crlf)、标记二进制文件跳过 diff(*.png binary)、为生成文件设置自定义 diff/merge 驱动,以及 export-ignore 从归档中排除文件。它解决仅靠 .gitconfig 无法处理的跨平台行尾问题。没有 .gitattributes,行尾行为取决于每位开发者的全局配置,导致嘈杂 diff 与损坏文件。有了它,仓库声明正确行为并由 Git 一致执行。
示例
bash
# .gitattributes file contents:
# Auto-detect text vs binary, normalize line endings in repo
* text=auto
# Force CRLF for Windows batch files
*.bat text eol=crlf
*.cmd text eol=crlf
# Force LF for shell scripts
*.sh text eol=lf
# Mark binaries — no diff, no text conversion
*.png binary
*.zip binary
# Custom merge for generated lockfile
package-lock.json merge=ours* text=auto 规范化行尾。eol=crlf/lf 强制特定行尾。binary 跳过 diff 与转换。merge=ours 冲突时保留我方版本。
相关术语
相关资源
相关课程
- Git 设置
学习 .gitattributes。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。