git crlf

跨平台的情况下,windows和unix的换行符谁不一样的。这个和git结合起来的时候有点烦哈。

如何将git的所有crlf文件转换成lf格式

将git库中的crlf转换为lf

下面这个脚本可以将库中的所有文件中的crlf去掉

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# From the root of your repository remove everything from the index
git rm --cached -r .

# Change the autocrlf setting of the repository (you may want
# to use true on windows):
git config core.autocrlf input

# Re-add all the deleted files to the index
# (You should get lots of messages like:
# warning: CRLF will be replaced by LF in <file>.)
git diff --cached --name-only -z | xargs -0 git add

# Commit
git commit -m "Fixed crlf issue"

# If you're doing this on a Unix/Mac OSX clone then optionally remove
# the working tree and re-check everything out with the correct line endings.
git ls-files -z | xargs -0 rm
git checkout .

gitattribute

github上面的关于crlf的处理

其中一个重点,可以在git库里面加入.gitattributes文件处理crlf的问题

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

you’ll notice that files are matched–*.c, *.sln, *.png–, separated by a space, then given a setting–text, text eol=crlf, binary. We’ll go over some possible settings below.

text=auto

Git will handle the files in whatever way it thinks is best. This is a good default option.

text eol=crlf

Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux. For example, here is a Windows project that enforces CRLF line endings.

text eol=lf

Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows. For example, here is a project that enforces LF line endings.

binary

Git will understand that the files specified are not text, and it should not try to change them. The binary setting is also an alias for -text -diff.

另外一个是: Refreshing a repository after changing line endings

这部分和上面第一节是基本一样的内容,(前提是设置了core.autocrlf或.gitattributes文件)

配置core.autocrlf

core.autocrlf的解释

How core.autocrlf works:

1
2
3
4
5
6
7
8
9
10
11
core.autocrlf=true:    core.autocrlf=input:      core.autocrlf=false:

repo repo repo
/ \ / \ / \
crlf->lf lf->crlf crlf->lf \ / \
/ \ / \ / \


Reminder: crlf = win-style end-of-line marker, lf = unix-style.

Note that cr (mac-style) in not affected for any of three options above.

tips: 建议搭建使用编辑器的时候,如果是windows下,特别关注一下回车的格式 提交到代码仓库的时候,按照unix的格式提交。例如上面的core.autocrlf=input。 如果想在checkout的文件为crlf,就设置为core.autocrlf=true