按照我的理解,如果设置core.autocrlf为true,在commit时会将“CRLF”换行符转为“LF”保存在仓库,checkout时又转换回来。然而实际测试似乎不是这么回事。百思不得姐。

core.autocrlf参数的官方帮助说明

根据《Pro Git》,

core.autocrlf
假如你正在 Windows 上写程序,而你的同伴用的是其他系统(或相反),你可能会遇到 CRLF 问题。这是因为Windows 使用回车(CR)和换行(LF)两个字符来结束一行,而 Mac 和 Linux 只使用换行LF)一个字符。虽然这是小问题,但它会极大地扰乱跨平台协作。许多 Windows 上的编辑器会悄悄把行尾的换行字符转换成回车和换行,或在用户按下 Enter 键时,插入回车和换行两个字符。

Git 可以在你提交时自动地把回车和换行转换成换行,而在检出代码时把换行转换成回车和换行。你可以用core.autocrlf 来打开此项功能。如果是在 Windows 系统上,把它设置成 true,这样在检出代码时,换行会被转换成回车和换行:

git自带帮助的说法不是很直接,基本意思该还是和上面的说法不矛盾,

core.autocrlf
Setting this variable to “true” is the same as setting the text attribute to “auto” on all files and core.eol to “crlf”. Set to true if you want to have CRLF line endings in your working directory and the repository has LF line endings. This variable can be set to input, in which case no output conversion is performed.

按照我的理解,如果设置core.autocrlf为true,在commit时会将“CRLF”换行符转为“LF”保存在仓库,checkout时又转换回来。仓库中始终使用“LF”换行,工作区中始终使用“CRLF”换行。下面看看实际测试的结果。

测试情况

xxx目录下包含read.win和read.unix两个文本文件,内容“相同”(仅换行风格不同,分别为“CRLF”和“LF”)。首先创建git仓库。

1
2
3
paul@paul-PC MINGW64 /e/xxx
$ git init
Initialized empty Git repository in E:/xxx/.git/

查询当前设置,core.autocrlf已设置为true(Windows平台下的默认值)。

1
2
3
4
5
6
7
8
9
10
11
paul@paul-PC MINGW64 /e/xxx (master)
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true

添加当前目录到staging aera并提交,给出的提示是read.unix中的“LF”将被转换为“CRLF”,这与我的预期(read.win中的“CRLF”转换为“LF”,read.unix保持不变)相反。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
paul@paul-PC MINGW64 /e/xxx (master)
$ git add .
warning: LF will be replaced by CRLF in read.unix.
The file will have its original line endings in your working directory.
paul@paul-PC MINGW64 /e/xxx (master)
$ git commit -m "initial"
[master (root-commit) 7eb8f08] initial
2 files changed, 6 insertions(+)
create mode 100644 read.unix
create mode 100644 read.win
paul@paul-PC MINGW64 /e/xxx (master)
$ git status
On branch master
nothing to commit, working tree clean

使用Git Windows安装包中自带的dos2unix将read.win文件换行符转换为“LF”,status命令检测到了文件变化,diff命令给出了提示,但执行commit时,git表示没有需要提交的内容(此时使用NotePad++打开read.win进行验证,换行符确实已转换为unix风格)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
paul@paul-PC MINGW64 /e/xxx (master)
$ dos2unix.exe read.win
dos2unix: converting file read.win to Unix format...
paul@paul-PC MINGW64 /e/xxx (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: read.win
no changes added to commit (use "git add" and/or "git commit -a")
paul@paul-PC MINGW64 /e/xxx (master)
$ git diff
warning: LF will be replaced by CRLF in read.win.
The file will have its original line endings in your working directory.
paul@paul-PC MINGW64 /e/xxx (master)
$ git add .
warning: LF will be replaced by CRLF in read.win.
The file will have its original line endings in your working directory.
paul@paul-PC MINGW64 /e/xxx (master)
$ git commit -m "change eol to lf in read.win"
On branch master
nothing to commit, working tree clean
paul@paul-PC MINGW64 /e/xxx (master)
$ git log
commit 7eb8f08b1f16b68cc8e41eefc4d51fb4757cf505 (HEAD -> master)
Author: paul <orgeup@gmail.com>
Date: Thu Sep 7 11:29:39 2017 +0800
initial

使用NotePad++,将read.unix换行符由unix风格转换为dos风格,status命令检测到了文件变化,diff/status未提示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
paul@paul-PC MINGW64 /e/xxx (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: read.unix
no changes added to commit (use "git add" and/or "git commit -a")
paul@paul-PC MINGW64 /e/xxx (master)
$ git diff
paul@paul-PC MINGW64 /e/xxx (master)
$ git add read.unix
paul@paul-PC MINGW64 /e/xxx (master)
$ git status
On branch master
nothing to commit, working tree clean

Windows平台下的开发工具(VS2010, QtCreator),似乎都没有设置文本编辑器换行风格的选项,有点小纠结。