1. 程式人生 > >git 處理和修改行結束符(CRLF和LF)

git 處理和修改行結束符(CRLF和LF)

目錄:

  1. 什麼是CRLF和LF
  2. 為什麼要探究CRLF和LF
  3. 三種方式處理的不同
  4. 更多
  5. 參考文獻

1、什麼是CRLF和LF

CRLF 是carriagereturnlinefeed的縮寫。中文意思是回車換行。

LF是line feed的縮寫,中文意思是換行。

2、為什麼要探究CRLF和LF

在學習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)。

為什麼會出現這三種處理line endings(行尾結束符)呢?在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 a line 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和GitHub

3、三種方式處理的不同

CRLF->Windows-style

LF->Unix Style

CR->Mac Style

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

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

CR表示只使用回車.

4、在Git中如何轉換?

在Git通過下面的命令配置

$git config --global core.autocrlf true
# 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。【2】

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。【2】

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

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

這是參考文獻2給的解釋希望能幫助大家。 

Yet another way to show how autocrlf works

1) true:             x -> LF -> CRLF
2) input:            x -> LF -> LF
3) false:            x -> x -> x

where x is either CRLF (windows-style) or LF (unix-style) and arrows stand for

file to commit -> repository -> checked out file

更多: 

更為複雜的配置命令見網站:https://www.kernel.org/pub/software/scm/git/docs/git-config.html

關於LF和CRLF討論見:http://stackoverflow.com/questions/1967370/git-replacing-lf-with-crlf 

You can also provide a special --global flag, which makes Git usethe same settings for line endings across every local Git repository on your computer.

--------------------------------------------------------------------------------------------------------------------------------------------------------

  解決不同平臺下結束符差別導致的各種問題,需要通過設定 core.autocrlf 來搞定。兩種可能遇到的提示資訊:

warning: LF will be replaced by CRLF 
fatal: CRLF would be replaced by LF
假如你正在 Windows 上寫程式,又或者你正在和其他人合作,他們在 Windows 上程式設計,而你卻在其他系統上,在這些情況下,你可能會遇到行尾結束符問題。這是因為 Windows 使用回車和換行兩個字元來結束一行,而 Mac 和 Linux 只使用換行一個字元。雖然這是小問題,但它會極大地擾亂跨平臺協作。

      Git 可以在你提交時,自動地把行結束符 CRLF 轉換成 LF,而在簽出程式碼時把 LF 轉換成 CRLF 。用 core.autocrlf 來開啟此項功能,如果是在Windows 系統上,把它設定成 true,這樣當簽出程式碼時,LF 會被轉換成 CRLF:
git config --global core.autocrlf true
      Linux 或 Mac 系統使用 LF 作為行結束符,因此你不想 Git 在簽出檔案時進行自動的轉換;當一個以 CRLF 為行結束符的檔案不小心被引入時,你肯定想進行修正,把 core.autocrlf 設定成 input 來告訴 Git 在提交時把 CRLF 轉換成 LF,簽出時不轉換:
git config --global core.autocrlf input

參考上面的配置方法,你就可以在 Windows 系統上,簽出檔案時保留 CRLF,而在 Mac 和 Linux 系統上,包括倉庫中,保留 LF 。

      如果你是 Windows 程式設計師,且正在開發僅執行在 Windows 上的專案,可以設定 false 取消此功能,把回車符記錄在庫中:
git config --global core.autocrlf false

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------