1. 程式人生 > >Git自動換行符

Git自動換行符

rri 關閉 his 工具 size 打開 printf 目前 輸出

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自動換行符