techiall / Blog

🍋 [My Blog] See discussions
https://github.com/techiall/Blog/discussions
MIT License
8 stars 1 forks source link

Git 换行符 #50

Open techiall opened 5 years ago

techiall commented 5 years ago

Git 换行符

操作系统 换行符
Unix 和 OS X \n ,0x0A(LF)
Windows \r\n,0x0D0A(CRLF)
早期 Mac OS \r,0x0D(CR)

在 Git 中可以使用 AutoCRLFSafeCRLF 对换行符进行设置。

以下是我个人使用配置。

# 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true

# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input

在多人团队协作中,我们可以使用添加 .gitattributes 文件来限定换行符。这个文件通常放在项目的根目录,也就是有 .git 文件夹的那个目录。

.gitattributes 每一行格式如下

要匹配的文件模式 属性1 属性2 ...

因此我们用如下格式来限定换行符

# 对于任意文件,当该文件的格式为 text,即文本时,在提交的时候,行尾自动转换成 lf。
* text eol=lf

参考文档

techiall commented 5 years ago

另外也记录一下这个问题引申出来的其他有意思的问题。