windows換行符號和Unix換行符
前言
Windows和Unix(MACOS,Linux)是完全不一樣的操作系統
macOS,Linux都是類Unix系統,所以他們存在Unix的配置習慣; 而 windows系統准確是DOS系統
是全新的系統,Windows和Unix有着本質差別
最近,使用git推送項目,遇到之前常見的問題,雖然不是很嚴重,但是確是影響視覺體驗。

問題如下:
The file will have its original line endings in your working directory. warning: LF will be replaced by CRLF in xxxxx.
說的就是 “ 這個文件有它自己原始的行結束在工作區時,警告: LF被CRLF替換 在xxx文件”
說道這,就需要了解 Unxi系統和Windows系統一些微妙的差距
在電腦系統中,如果文件中的內容需要換行,一般是\n
,這點在Unix系統是體現的
然而在Windows系統上,出奇的多了一個\r
認識概念
1、符號
換行符(LR): line feed簡稱
回車符(CR): carriage return的簡稱
換行和回車符實際在文件中的直觀感受是這樣:

是的
在windows系統中,換行用CRLE
, 回車+換行符
在Unix系統中,換行用LF
,即換行符
2、自動轉換
core.autolf是什么?
Formatting and whitespace issues are some of the more frustrating and subtle problems that many developers encounter when collaborating, especially cross-platform. It’s very easy for patches or other collaborated work to introduce subtle whitespace changes because editors silently introduce them, and if your files ever touch a Windows system, their line endings might be replaced. Git has a few configuration options to help with these issues.
翻譯:
格式和空格經常是開發者在合作交流中遇到的煩人和容易忽視的問題,特別是在跨平台合作中,很容易采用微妙的空格對於補丁或者其他的合作的作品。因為編輯器會稍微采取這些措施,如果你曾經在windows平台創建文件,這些文件的行可能會被替換,Git有一些配置選項去幫助解決這些問題。
所以core,autolf
就是說,git
會幫助自動的將CRLF
轉化為LF
設置為true
就是轉換
$ git config --global core.autocrlf true
If you’re programming on Windows and working with people who are not (or vice-versa), you’ll probably run into line-ending issues at some point. This is because Windows uses both a carriage-return character and a line feed character for newlines in its files, whereas Mac and Linux systems use only the line feed character. This is a subtle but incredibly annoying fact of cross-platform work; many editors on Windows silently replace existing LF-style line endings with CRLF, or insert both line-ending characters when the user hits the enter key.
Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true – this converts LF endings into CRLF when you check out code:
$ git config --global core.autocrlf true
這里需要認清兩個區域
- Windows文件系統 和 git(Unix系統)
如果是進行git操作,其實是涉及了Unix系統配置,Linux也是類Unix系統
所以此時從Windows系統(工作區) ➡ Unix系統(暫存區(index)或者版本庫(repository))會經歷 CRLF
➡ LF
的轉變
如果涉及從 git(此時指的是Unix系統配置) ➡ windows本地文件系統(此時指的是windows配置)
會經歷 LF
➡ CRLF
的轉變
這是由編輯器決定的
如果設置為
$ git config --global core.autocrlf true
則會git會根據你的git操作(git add, git commit等),轉換 符號
如果設置為
$ git config --global core.autocrlf false
則不自動轉換
如果是windows系統,在windows文件系統中是CRLF
,提交到版本庫,也是CRLF
If you’re a Windows programmer doing a Windows-only project, then you can turn off this functionality, recording the carriage returns in the repository by setting the config value to false:
$ git config --global core.autocrlf false
詳情: https://stackoverflow.com/a/5834094/12341467
所以針對最初的問題:
就是說原本在windows文件系統區域的文件存在Unix配置LF
然后git add,git commit就會轉換(替換)為CRLF
即The file will have its original line endings in your working directory. warning: LF will be replaced by CRLF in xxxxx.

相關文章: