天天看點

CRLF LF的差別、git core.autocrlf的使用1. 什麼是CRLF和LF2. Git 對CRLF和LF換行符的處理3. Git autocrlf 的使用

1. 什麼是CRLF和LF

由于曆史的原因,各種不同的作業系統在處理行尾結束符采取了不同的處理方法。

CRLF 是carriagereturnline feed的縮寫。中文意思是回車換行。LF是line feed的縮寫,中文意思是換行。

CRLF->Windows-style

LF->Unix Style

CR->Mac Style

CRLF表示句尾使用回車換行兩個字元(即我們常在Windows程式設計時使用"\r\n"換行)

LF表示表示句尾,隻使用換行.

CR表示隻使用回車.

2. Git 對CRLF和LF換行符的處理

  • 首先說一下Git為什麼會對換行符做處理

在Git的幫助頁面給出了很好的解釋。

Reference From:https://help.github.com/articles/dealing-with-line-endings

If you're using Git to collaborate with others on GitHub, ensure that Git isproperly configured to handle line endings.

Every time you press return on your keyboard you're actuallyinserting an invisible character called aline ending. Historically, differentoperating systems have handled line endings differently.

When you view changes in a file, Git handles line endings in its own way.Since you're collaborating on projects with Git and GitHub, Git mightproduce unexpected results if, for example, you're working on a Windows machine,and your collaborator has made a change in OS X.

大概意思就是不同的系統會産品不同的換行符,而不同的人可能用不同的系統;為了更高效的與他人合作,Git會對換行符做統一的處理;

在學習git軟體,安裝git到configuring the lien ending conversion時,有三個選項。

a. Checkout Windows-style,commit Unix-style line endings.

b.Checkout as-is,commit Unix-style line endings.

c.Checkout as-is,commit as-is line endings.

這裡面講到了做兩個操作(Checkout,Commit)的三種處理line endings的操作(Windows-style,Unix-style,As-is)。

3. Git autocrlf 的使用

在Git通過下面的指令配置

$git config --global core.autocrlf true

# Configure Git on Windows to properly handle line endings

# Configure Git on Windows to properly handle line endings

解釋:core.autocrlf是git中負責處理line endings的變量,可以設定三個值--true,inout,false.

設定成三個值會有什麼效果呢?

If core.autocrlf is set to true, that means that any time you add a file to the git repo that git thinks is a text file, it will turn all CRLF line endings to just LF before it stores it in the commit.。

設定為true,添加檔案到git倉庫時,git将其視為文本檔案。他将把crlf變成lf。

If core.autocrlf is set to false, no line-ending conversion is ever performed, so text files are checked in as-is. This usually works ok。

設定為false時,line-endings将不做轉換操作。文本檔案保持原來的樣子。

設定為input時,添加檔案git倉庫時,git把crlf程式設計lf。當有人Check代碼時還是lf方式。是以在window作業系統下,不要使用這個設定。

轉換如表格:

core.autocrlf file to commit repository checked out file
true X LF CRLF
input X LF LF
false X X X

X 為LF或者CRLF