title | created_at | tag | toc | displayed_on_home | ||
---|---|---|---|---|---|---|
[Git] .gitattributes |
2020-01-16 12:52:01 -0800 |
|
true |
true |
位于Git Repo根目录的.gitattributes
文件,用来为Git管理的文件配置一些属性。这些属性控制着Git管理的如下三个区域的文件。
通常主要用来统一EOL=end of line
(在Windows上默认是crlf
, 在Linux和macOS上则是lf
)。
.gitattributes
是一个文本文件,每一行使用pattern
匹配一些文件,然后设置对应的属性:
pattern attr1 attr2 ...
每一个属性都遵循如下4种规则进行配置:
# 设置attr1,表示true
pattern attr1
# 未设置attr1,表示false
pattern -attr1
# 设置attr1的值为1
pattern attr1=1
# 未指定任何值
pattern
text
属性指示Git如何处理 .git directory 区域中的文本文件的EOL。比如:
# 使用lf存储*.sh文件。
*.sh text
# 不控制*.sh文件的EOL。
*.sh -text
eol
属性指示Git如何处理 working directory 区域中的文本文件的EOL。比如:
# 使用lf存储*.sh文件, 在working directory则继续使用lf。
*.sh text eol=lf
# 使用lf存储*.cs文件, 在working directory则自动转换为crlf。
*.cs text eol=crlf
内置的宏属性binary
等价于-diff -merge -text
当前站点项目所使用的配置如下:
{{}}
这样以来存储到Git中的文本文件都统一采用lf
,而针对个别文件在工作区中采用crlf
(亦可灵活的调整工作区中的EOL,而无需改动存储区已经存在的文件),需同时进行如下设置
# 阻止Git进行自动转换
git config --global core.autocrlf false
# 当提交的文件不符合.gitattributes的配置时,阻止git add命令
git config --global core.safecrlf true
.gitattributes
文件加入git仓库中后,需要执行一下如下命令:
git add --renormalize .
上述命令会检查git仓库中不符合配置的一些文件,然后再把这些文件commit到仓库中即可。
https://git-scm.com/docs/gitattributes
https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings