Skip to content

Latest commit

 

History

History
96 lines (63 loc) · 2.44 KB

index.md

File metadata and controls

96 lines (63 loc) · 2.44 KB
title created_at tag toc displayed_on_home
[Git] .gitattributes
2020-01-16 12:52:01 -0800
Git
gitattributes
true
true

1 .gitattributes 的作用 {#effects}

位于Git Repo根目录的.gitattributes文件,用来为Git管理的文件配置一些属性。这些属性控制着Git管理的如下三个区域的文件。

Git 三个工作区域

通常主要用来统一EOL=end of line(在Windows上默认是crlf, 在Linux和macOS上则是lf)。

1.1 语法 {#syntax}

.gitattributes是一个文本文件,每一行使用pattern匹配一些文件,然后设置对应的属性:

pattern attr1 attr2 ...

每一个属性都遵循如下4种规则进行配置:

# 设置attr1,表示true
pattern attr1

# 未设置attr1,表示false
pattern -attr1

# 设置attr1的值为1
pattern attr1=1

# 未指定任何值
pattern 

1.2 text 属性 {#text-attribute}

text属性指示Git如何处理 .git directory 区域中的文本文件的EOL。比如:

# 使用lf存储*.sh文件。
*.sh text

# 不控制*.sh文件的EOL。
*.sh -text

1.3 eol 属性 {#eol-attribute}

eol属性指示Git如何处理 working directory 区域中的文本文件的EOL。比如:

# 使用lf存储*.sh文件, 在working directory则继续使用lf。
*.sh  text eol=lf

# 使用lf存储*.cs文件, 在working directory则自动转换为crlf。
*.cs  text eol=crlf

2 MACRO 属性 {#macro-attribute}

内置的宏属性binary等价于-diff -merge -text

3 最佳实践 {#best-practice}

当前站点项目所使用的配置如下:

{{}}

这样以来存储到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到仓库中即可。

4 参考 {#reference}

https://git-scm.com/docs/gitattributes

https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings