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:

  1. 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=auto Allow Git to process automatically, or *.js text eol=lf Force JS file to use LF.

  2. 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.

  3. 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 -diff or specify a custom diff tool. For example:*.lock binary or *.ipynb diff=jupyternotebook.

  4. Control of merger behaviour Specifies that a document always uses a version when it is merged (e. g. ours or theirs). For example:config.xml merge=ours.

  5. 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.

  6. 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

Good and Bad PNG

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