http://blog.csdn.net/jonathan321/article/details/51988242?locationNum=2

不同的操作系统有不同的换行符格式,跨平台协作时需要考虑版本工具(git)对换行符的处理<!--more-->

回车和换行


回车(Carriage Return)和换行(Line Feed)概念:

  • 回车CR:将光标移动到当前行开头;
  • 换行LF:将光标“垂直”移动到下一行,并不改变光标水平位置。

以上的概念只适用于打字机,现代计算机沿用的时候主要使用的是回到行首换行+回到行首的功能。看下面的例子:

1、在Windows下应用程序输出\n到文件,会被自动转换成\r\n

// output:
// first line
// second line
printf("first line\nsecond line"); // test.txt output;
// first line\r\nsecond line
std::fstream fout("test.txt", std::ios::out);
if (fout.is_open())
{
fout.write("first line\nsecond line", sizeof("first line\nsecond line"));
fout.close();
}

2、在windows下应用程序输出\r到文件,不会被转换,并且并不会起到“将光标移动到当前行开头”的作用

// output:
// second line
printf("first line\rsecond line"); // test.txt output;
// first line\rsecond line
std::fstream fout("test.txt", std::ios::out);
if (fout.is_open())
{
fout.write("first line\rsecond line", sizeof("first line\rsecond line"));
fout.close();
}

3、在Windows下应用程序输出\r\n到文件,\r\n会被自动转换成\r\r\n

// output:
// first line
// second line
printf("first line\r\nsecond line"); // test.txt output;
// first line\r\r\nsecond line
std::fstream fout("test.txt", std::ios::out);
if (fout.is_open())
{
fout.write("first line\r\nsecond line", sizeof("first line\r\nsecond line"));
fout.close();
}

不同系统下的换行符


CR、LF、CR/LF为不同操作系统上使用的换行符:

  • Windows/DOS系统:采用CR/LF表示下一行;
  • Unix/Linux系统:采用LF表示下一行;
  • Mac OS系统:采用CR表示下一行;
  • Mac OS X系统:采用LF表示下一行(Mac OS X已经改成和Unix/Linx一样使用LF)。

CR使用符号'\r',十进制ASCII代码是13,十六进制代码为0x0D;LF使用'\n'符号表示,ASCII代码是10,十六制为0x0A。所以Windows平台上换行在文本文件中是使用 0d 0a 两个字节表示,而UNIX和苹果平台上换行则是使用 0a 或 0d 一个字节表示。

Unix/Linux/Mac系统下的文件在Windows里打开的话(使用Windows自带记事本),会出现换行丢失,所有文字会变成一行,整个文本会乱成一团。Windows系统下的文件在Unix/Linux/Mac里打开的话,在每行的结尾可能会多出一个^M符号。

目前大部分的编辑器和IDE都支持这几种换行符(除了notepad),但是跨平台协作项目源码到底保存为哪种风格的换行符呢?输出的文本需要保存为哪种风格的换行符呢?Git提供了一个解决方案——在跨平台协作场景时,会提供一个“换行符自动转换”的功能。

Git CRLF


Git默认在提交时将Windows换行符(CRLF)转换为LF,在拉取时将UNIX换行符(LF)替换成CRLF。我们可以通过设置autocrlf和safecrlf来设置具体的操作。

autocrlf and saftcrlf

1、autocrlf

// 提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true // 提交时转换为LF,检出时不转换
git config --global core.autocrlf input // 提交检出均不转换
git config --global core.autocrlf false

2、safecrlf

// 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true // 允许提交包含混合换行符的文件
git config --global core.safecrlf false // 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

.gitattributes

.gitattributes文件能够设置每个仓库的换行符配置,摘取Link中的设置为例:

###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto ###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.vcxproj merge=binary ###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary ###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

1、text=auto:采用git认为最好的方式来处理文件,未在.gitattributes中设置的项默认按照这种方式处理;

2、text eol=crlf/lf:在checkout时,转换Line Ending为crlf/lf;

3、binary: 告诉git该文件为二进制,防止git修改该文件。

注意:.gitattributes文件必须要提交之后才能生效。

由于目前Jenkins推送到打包服务器上的代码默认采用LF结尾,所以建议仓库内创建.gitattributes文件并设置。

项目实施一


设置原则

本地仓库完全一致,适合单一平台编程

团队设置

一个团队需要使用同一的换行符标准(UNIX标准或者Windows标准),然后配置自己的代码编辑器和IDE,达到两项要求:

  • 在新建文件时默认使用团队统一的换行符标准;
  • 在打开文件时保持现有换行符格式不变(不要做自动转换)。

Git设置

1、关闭换行符自动转换功能

// 提交检出均不转换
git config --global core.autocrlf false

2、开启换行符检查功能(按照需求设置)

// 拒绝提交包含混合换行符的文件
git config --global core.safecrlf true // 允许提交包含混合换行符的文件
git config --global core.safecrlf false // 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

留意每次提交

如果提交的时候变更行数过多(超过自己修改),或者增减行数相同,很有可能是整个文件的换行符被修改了,这个时候就要注意检查了。

项目实施二


设置原则

保证仓库永远换行符永远采用UNIX标准(LF),在Windows工作空间设置为Windows标准(CRLF),在Mac/Linux工作空间设置为Unxi标准(LF),适合跨平台编程

团队设置

统一不同平台下的换行符标准,按照上面设置原则的标准,配置自己的代码编辑器和IDE,,达到两项要求:

  • 在新建文件时默认使用团队统一的换行符标准;
  • 在打开文件时保持现有换行符格式不变(不要做自动转换)。

Git设置

1、设置换行符自动转换功能

# Configure Git on OS X or Linux to properly handle line endings
git config --global core.autocrlf input # Configure Git on Windows to properly handle line endings
git config --global core.autocrlf true

2、设置换行符检查功能

// 提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

留意每次提交

1、留意每次提交的更改行数。

2、留意提交时的换行符警告。

Git自动换行符的更多相关文章

  1. git 换行符LF与CRLF转换问题

    git 换行符LF与CRLF转换问题 一.背景 在各操作系统下,文本文件所使用的换行符是不一样的.UNIX/Linux 使用的是 0x0A(LF),早期的 Mac OS 使用的是0x0D(CR),后来 ...

  2. git 换行符问题

    git 换行符问题 在windows环境中 对于autocrlf = false 不会激发 关于换行符的处理 对于autocrlf = true 会在提交是将LF替换成CRLF 切出时时CRLF 对于 ...

  3. git换行符之autoCRLF配置的意义

    关于git换行符处理的问题,我查了一查,自己的设置中,global-config中设了autocrlf=false,systemwide中将autocrlf设成了true. 关于配置的作用域,syst ...

  4. Git换行符是如何精确控制的

    Git换行符是如何精确控制的 Checkout Windows-style, commit Unix-style Git will convert LF to CRLF when checking o ...

  5. Git 换行符检查 CRLF 与 LF

    遇到的问题 在 git 提交或是签出时,提示如下问题: [git] warning: LF will be replaced by CRLF | fatal: CRLF would be replac ...

  6. git换行符自动转换导致整个文件被修改的解决方案

    不少开发者可能遇到过这个问题:从git上拉取服务端代码,然后只修改了一处地方,准备提交时,用diff软件查看,却发现整个文件都被修改了.这是git自动转换换行符导致的问题. 原因 不同操作系统使用的换 ...

  7. 解决不同操作系统下git换行符一致性问题

    一.不同操系统下的换行符CR回车 LF换行Windows/Dos CRLF \r\nLinux/Unix LF \nMacOS CR \r二.解决方法 打卡git bash,设置core.autocr ...

  8. git换行符问题

    from: http://www.cnblogs.com/flying_bat/archive/2013/09/16/3324769.html 一.AutoCRLF#提交时转换为LF,检出时转换为CR ...

  9. vscode wsl git 换行符问题autocrlf

    wsl中使用code,由于windows换行符问题git会显示大量文件修改,此时需要在wsl中设置autocrlf设置 git config --global core.autocrlf input ...

随机推荐

  1. 浅谈history对象以及路由插件原理

    简介 History对象最初设计用来表示窗口的浏览历史,但是,出于隐私方面的原因,History对象不再允许脚本访问已经访问过的实际URL.虽然,我们不清楚历史URL,但是,我们可以通过History ...

  2. 前端学习笔记之CSS文档流

    先引用一段W3C的文档: 9.3 Positioning schemes In CSS 2.1, a box may be laid out according to three positionin ...

  3. 常用<meta>

    转自:http://segmentfault.com/a/1190000002407912 w3c -- <meta>标签:http://www.w3school.com.cn/tags/ ...

  4. Migrating from Spring 3 to Spring 4 - org.springframework.scheduling.quartz.CronTriggerBean

    I'm trying to migrate from spring 3.0.5 to spring 4.1.X . Spring 3 has Class named as "org.spri ...

  5. ubuntu18.04系统安装+基本环境配置【原创】

    平台信息:PC:ubuntu18.04.i5.七彩虹GTX1060显卡.固态硬盘.机械硬盘 作者:庄泽彬(欢迎转载,请注明作者) 说明:在原本的电脑买一个独立显卡,装上去之后,出了各种问题,虽然后面勉 ...

  6. 《Effective Java 2nd》第7章 方法

    目录 第38条 检查参数的有效性 第39条 必要时进行保护性拷贝 第40条 谨慎设计方法签名 第41条 慎用重载 第42条 慎用可变参数 第43条 返回零长度的数组或集合,而不是null 第44条 为 ...

  7. 解决com.mongodb.MongoException$CursorNotFound: cursor 0 not found on server

    背景 经常需要执行脚本调用Java程序读取mongodb中数据,本来是转为后台进程.偶尔看看日志的简单任务.今天发现程序抛出异常“com.mongodb.MongoException$CursorNo ...

  8. Codeforces Round #401 (Div. 2) A,B,C,D,E

    A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...

  9. Facade(外观)

    意图: 为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 适用性: 当你要为一个复杂子系统提供一个简单接口时.子系统往往因为不断演化而变 ...

  10. Qt5窗口标题栏高度

    1.frameGeometry().height() - geometry().height() 2. QRect desktopRect = QApplication::desktop()-> ...