tool-skills/git-config/.gitattributes
fnzhang 71cf9e8cc2 feat(git-config): 新增推荐 .gitattributes(换行符/编码属性模板)
从文档站《Git for Windows》笔记的代码块提取,供直接下载/引用。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-28 18:42:48 +08:00

78 lines
4.8 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# https://git-scm.com/docs/gitattributes
# https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
# https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-config.html
# 回车换行默认值声明 ---------------------------------------------------------------------------------
# 名词说明 check in: 对应 commit, check out: 对应 cherry-pick, pull 等
# 笔记中回车换行的基本原则:(下文 core.xxx 可以认为是 .gitatrributes 中未显示指定时,使用的默认回车换行类型)
# checkout 时,回车换行不变: git config core.autocrlf false
# false: If you're a Windows programmer doing a Windows-only project, then you can turn off this functionality, recording the carriage returns in the repository by setting the config value to false
# true: If you're on a Windows machine, set it to true — this converts LF endings into CRLF when you check out code, and convert CRLF endings into LF when you check in code.
# input: If you're on a Linux or macOS system that uses LF line endings, You can tell Git to convert CRLF to LF on commit and convert CRLF to LF on pull, by setting core.autocrlf to input.
# 对于跨平台发开同同时不同平台只能以不同的后缀编译代码时windows 平台设置 truelinux 平台设置 input。需要注意的是由于 Linux 用户通常不会遇到 CRLF 换行符,因此 input 设置主要是为了防止从其他平台(如 Windows检入的代码中出现换行符问题。
# commit 时,
# core.eol 仅在 core.autocrlf false 生效。 笔记默认让有 text 属性(包括 git 自动判断有 text 属性)的回车换行使用 core.eol 设置值。
# Sets the line ending type to use in the working directory for files that have the text property set when core.autocrlf is false. Alternatives are lf, crlf and native, which uses the platform's native line ending. The default value is native. See gitattributes[5] for more information on end-of-line conversion.
# .gitattributes 匹配优先级 -------------------------------------------------------------------------
# 1、Git 仓库的 .git/info/ 目录下,其内的属性设置具有最高的优先级,会覆盖其他位置的相同属性设置。
# 2、离文件越近的 .gitattributes 文件中的规则优先级越高。然后再是由 Git 配置变量 core.attributesFile 指定的文件。之后再是 系统范围属性文件:通常位于 /etc/gitattributes。
# 3、在同一个 .gitattributes 文件中,后面的规则会覆盖前面的规则。
# 属性说明 ------------------------------------------------------------------------------------------
# text:
# 1. set:
# a. When a matching file is added to the index, the file's line endings are normalized to LF in the index every time the file is checked in, even if the file was previously added to Git with CRLF line endings.
# b. When the file is copied from the index to the working directory, its line endings may be converted from LF to CRLF depending on the eol attribute, the Git config, and the platform (see explanation of eol below).
# 2. Unset, e.i. `-text`. Unsetting the text attribute on a path tells Git not to attempt any end-of-line conversion upon checkin or checkout.
# 3. `text=auto`: When text is set to "auto", Git decides by itself whether the file is text or binary.
# 4. `Unspecified`: If the text attribute is unspecified, Git uses the core.autocrlf configuration variable to determine if the file should be converted.
#
# elf:
# 1、 Unspecified:
#
# c、 If text is set but neither of those variables is, the default is eol=crlf on Windows and eol=lf on all other platforms.
# 具体属性如下 --------------------------------------------------------------------------------------
* -text
*.txt text
*.vcproj text eol=crlf
*.bat text eol=crlf ecoding=gbk
*.tat text eol=crlf
*.md text eol=crlf
*.ps1 text working-tree-encoding=UTF-16LE eol=crlf
*.sh text eol=lf
*.cmake text eol=lf
*.cpp text eol=lf
*.c text eol=lf
*.h text eol=lf
*.hpp text eol=lf
*.css text eol=lf
*.puml text eol=lf
*.py text eol=lf
*.xml text eol=lf
*.yaml text eol=lf
*.jpg -text
*.png -text
*.ico -text
*.7z -text
*.zip -text
*.jar -text
*.gz -text
*.class -text
*.eddx -text
*.emmx -text
*.mp3 -text
*.pcm -text
*.pdf -text
*.so -text
*.xlsx -text
*.lsx -text