A Carefully Specified .gitattributes File
A strict Git project library should have.gitattributesDocumentation to allow Git to process documents according to the established rules in different operating systems and scene environments.
.gitattributesCommon uses of documents are:
-
Unified Line Ender (CRLF / LF) Prevents a large number of false changes resulting from inconsistent line breaks when Windows and Mac/Linux collaborates. For example:
* text=autoAllow Git to process automatically, or*.js text eol=lfForce JS file to use LF. -
Specify Binary File Avoids Git trying to merge or output a random code diff for binary files such as pictures, compressed packages, etc. For example:
*.png binary. -
Define diff policy Some generated files (e.g. locked files, compressed codes) do not want to look at normal diff, which can be marked as
-diffor specify a custom diff tool. For example:*.lock binaryor*.ipynb diff=jupyternotebook. -
Control of merger behaviour Specifies that a document always uses a version when it is merged (e. g.
oursortheirs). For example:config.xml merge=ours. -
Filter content (smudge / clean) Automatically executes scripts when checking out/submission, for example, to automatically replace a variable for a template file, and simply decrypts a key file. For example:
*.secret filter=crypt. -
Language statistics affecting GitHub Keep GitHub from including certain files (e.g., generated code, test data) in the language ratio of the item. For example:
generated.js linguist-generated=true.
Take, for example, the project I've been working on..gitattributesHere's the document:
# 1. 设置默认安全策略(放在最前面)
* text=auto
# 2. 对所有文本文件强制执行LF标准(覆盖全局)
# 这样能确保跨平台的基线统一
* text eol=lf
# 3. 为Windows专属文件,单独打破规则,强制使用CRLF(放在后面)
# 这行规则的优先级高于上面的 '* text eol=lf'
*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf
# 4. 明确标记所有二进制文件(放在最后,作为终极保障)
# 这些规则会覆盖前面的所有文本处理规则,确保安全
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.pdf binary
*.zip binary
*.mp3 binary
*.mp4 binary
*.mov binary
*.webp binary
*.bytes binary
# 5. LFS文件,对于大尺寸文件,要纳入LFS管理
# “-text”表示这个文件非文本文件
Assets/Resources/Data/Data.bytes filter=lfs diff=lfs merge=lfs -text
It's not good..gitattributesThere are potential problems.
Today, for example, after I found out that web's rear station had been redeployed, Logo broke and didn't show up. After a closer examination, we found local png files, and in Git's library, on the file. Major differences:
本地版本
89 50 4E 47 0D 0A 1A 0A
Git库内版本
89 50 4E 47 0A 1A 0A

Apparently, Git was submitting it.CRLFIt's changed.LF.