[toc]
一、遇到的問(wèn)題
在Windows平臺(tái)上塑娇,會(huì)出現(xiàn)如下的warning:
$ git add app.wxss
warning: LF will be replaced by CRLF in app.wxss.
The file will have its original line endings in your working directory.
為什么會(huì)出現(xiàn)這個(gè)warning呢澈侠?
二、為什么會(huì)出現(xiàn)這個(gè)問(wèn)題
通過(guò)查閱《ProGit》的相關(guān)內(nèi)容埋酬,可以得知:
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 linefeed character for newlines in its files, whereas Mac and Linux systems use only the linefeed 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.
簡(jiǎn)單翻譯一下哨啃,Windows在換行的時(shí)候,同時(shí)使用了回車符(carriage-return character)和換行符(linefeed character)写妥;而Mac和Linux系統(tǒng)(很老的mac系統(tǒng)才是CR棘催,可以參見(jiàn)wiki),此時(shí)耳标,僅僅使用了換行符(linefeed character)醇坝。同時(shí)呢,Windows的許多編輯器還悄悄滴將LF修改成了CRLF格式的行結(jié)束符次坡,或者在你敲回車的時(shí)候呼猪,CRLF格式的行結(jié)束符就產(chǎn)生了。當(dāng)然砸琅,這一切都發(fā)生在同時(shí)存在Windows和非Windows的跨平臺(tái)工作中宋距,如果大家都是同一種操作系統(tǒng),那么症脂,就天下太平了谚赎。
warning中所說(shuō)的LF和CRLF分別是linefeed和carriage-return&linefeed。
那么現(xiàn)在诱篷,再仔細(xì)看warning的內(nèi)容壶唤,為什么在git add
的時(shí)候,“警告:app.wxss中的LF將來(lái)會(huì)被替換成CRLF”棕所?
三闸盔、為什么LF->CRLF
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.
Git有一個(gè)針對(duì)性的功能:當(dāng)添加到暫存區(qū)時(shí),自動(dòng)將CRLF轉(zhuǎn)換成LF琳省;反之迎吵,當(dāng)檢出時(shí)躲撰,自動(dòng)將LF轉(zhuǎn)換成CRLF。
You can turn on this functionality with the core.autocrlf setting.
可以通過(guò)設(shè)置core.autocrlf
來(lái)開(kāi)啟這個(gè)功能击费。
看到這里拢蛋,就更加不懂了,添加到暫存區(qū)時(shí)蔫巩,為什么警告LF將被轉(zhuǎn)換成CRLF谆棱?這不是和功能相反了嗎?
四批幌、原因
這個(gè)問(wèn)題與git-config
里面的相關(guān)設(shè)置有關(guān)础锐,主要涉及到三個(gè)參數(shù):
core.autocrlf
core.safecrlf
core.eol
這三個(gè)參數(shù)用來(lái)配置Git處理line-ending的方式嗓节。
五荧缘、接下來(lái),來(lái)看如何配置
1. core.autocrlf
(1) true
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 系統(tǒng)上拦宣,把它設(shè)置成 true截粗,這樣在檢出代碼時(shí),換行(LF)會(huì)被轉(zhuǎn)換成回車和換行(CRLF)
(2) input
If you’re on a Linux or Mac system that uses LF line endings, then you don’t want Git to automatically convert them when you check out files; however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input:
$ git config --global core.autocrlf input
如果使用以換行作為行結(jié)束符的 Linux 或 Mac鸵隧,你不需要 Git 在檢出文件時(shí)進(jìn)行自動(dòng)的轉(zhuǎn)換绸罗;然而當(dāng)一個(gè)以回車加換行作為行結(jié)束符的文件不小心被引入時(shí),你肯定想讓 Git 修正豆瘫。 你可以把 core.autocrlf 設(shè)置成 input 來(lái)告訴 Git 在提交時(shí)把回車和換行轉(zhuǎn)(CRLF)換成換行(LF)珊蟀,檢出時(shí)不轉(zhuǎn)換
(3) false
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
如果你是 Windows 程序員,且正在開(kāi)發(fā)僅運(yùn)行在 Windows 上的項(xiàng)目外驱,可以設(shè)置 false 取消此功能育灸,把回車(CR)保留在版本庫(kù)中
以上,是core.autocrlf
的設(shè)置規(guī)則與建議昵宇。
2. core.safecrlf
通過(guò)查閱git-scm上的文檔上關(guān)于git-config
的文檔或者mirrors.edge.kernel.org
If true, makes Git check if converting
CRLF
is reversible when end-of-line conversion is active. Git will verify if a command modifies a file in the work tree either directly or indirectly.For example, committing a file followed by checking out the same file should yield the original file in the work tree. If this is not the case for the current setting ofcore.autocrlf
, Git will reject the file.
簡(jiǎn)單翻譯一下:當(dāng)core.autocrlf
為true
或者input
時(shí)磅崭,算激活了eol
,此時(shí)如果core.safecrlf
為true
瓦哎,Git檢查crlf
轉(zhuǎn)換是否正常砸喻,比如Windows平臺(tái),core.autocrlf
設(shè)置為true
蒋譬,如果工作區(qū)的文件中含有LF
割岛,Git就會(huì)拒絕,因?yàn)?code>true的情況下犯助,Git認(rèn)為工作區(qū)應(yīng)該都是CRLF
才對(duì)啊蜂桶。
給你一個(gè)致命大禮,fatal:LF would be replaced by CRLF in app.wxss.
也切,阻止你繼續(xù)操作扑媚。
^?_?^ 使用dos2unix進(jìn)行手動(dòng)轉(zhuǎn)換
這種情況不要慌腰湾,可以使用dos2unix工具中的unix2dos將LF
轉(zhuǎn)換成CRLF
,來(lái)滿足core.autocrlf
為true
的要求疆股。當(dāng)然费坊,解決的方法有很多,可以根據(jù)實(shí)際情況旬痹,使用不同的方法來(lái)解決附井。
以下命令可以將當(dāng)前文件夾內(nèi)的文件批量的轉(zhuǎn)換,也有其他方式两残,比如find . -type f -exec dos2unix {} +
永毅,可以自行Google
find . -type f -print0 | xargs -0 dos2unix
dos2unix工具在Windows的MINGW64 Git Bash客戶端上自帶,在mac平臺(tái)可以Google how to install dos2unix on mac人弓,同理linux(sudo apt-get install dos2unix
)沼死,哈哈哈。
The variable can be set to "warn", in which case Git will only warn about an irreversible conversion but continue the operation.
也可以設(shè)置core.safecrlf
為warn
崔赌,Git就只會(huì)警告一下意蛀,不會(huì)阻止你。
繼續(xù)看文檔的內(nèi)容健芭。寫(xiě)著寫(xiě)著成了《帶你看文檔系列》了县钥,所以很多問(wèn)題,仔細(xì)看文檔就能解決慈迈,但是文檔太多若贮,也很難看懂,只能花時(shí)間多看了痒留。
CRLF conversion bears a slight chance of corrupting data. When it is enabled, Git will convert CRLF to LF during commit and LF to CRLF during checkout. A file that contains a mixture of LF and CRLF before the commit cannot be recreated by Git. For text files this is the right thing to do: it corrects line endings such that we have only LF line endings in the repository. But for binary files that are accidentally classified as text the conversion can corrupt data.
If you recognize such corruption early you can easily fix it by setting the conversion type explicitly in .gitattributes. Right after committing you still have the original file in your work tree and this file is not yet corrupted. You can explicitly tell Git that this file is binary and Git will handle the file appropriately.
這一段主要是說(shuō)谴麦,同時(shí)有LF
和CRLF
的文件是被腐蝕的,這類文件是不被Git認(rèn)可的狭瞎,而CRLF的轉(zhuǎn)換過(guò)程很可能會(huì)產(chǎn)生腐蝕的文件细移,比如二進(jìn)制的文件被當(dāng)作文本的話,就會(huì)被腐蝕熊锭。
所以提出了重要的文件.gitattributes
來(lái)明確轉(zhuǎn)換配置弧轧,而且越早設(shè)置越好。你可以明確地指出某些文件是二進(jìn)制的碗殷,比如*.jpg精绎,Git知道了以后,會(huì)妥善處理锌妻。
Note, this safety check does not mean that a checkout will generate a file identical to the original file for a different setting of core.eol and core.autocrlf, but only for the current one. For example, a text file with LF would be accepted with core.eol=lf and could later be checked out with core.eol=crlf, in which case the resulting file would contain CRLF, although the original file contained LF. However, in both work trees the line endings would be consistent, that is either all LF or all CRLF, but never mixed. A file with mixed line endings would be reported by the core.safecrlf mechanism.
我的理解是代乃,A和B協(xié)同開(kāi)發(fā),A設(shè)置的是core.eol=lf core.autocrlf=true
,B設(shè)置的是core.eol=crlf core.autocrlf=true
搁吓,那么A checkout的時(shí)候原茅,生成lf
結(jié)尾的文件,而B(niǎo) checkout的時(shí)候堕仔,生成crlf
結(jié)尾的文件擂橘。要么都是lf
,要么都是crlf
摩骨,安全檢查不可能允許混合出現(xiàn)通贞,如果發(fā)現(xiàn)了混合的行結(jié)束符,就會(huì)報(bào)警
3. core.eol
通過(guò)查閱git-scm上的文檔上關(guān)于git-config
的文檔或者mirrors.edge.kernel.org
Sets the line ending type to use in the working directory for files that have the
text
property set when core.autocrlf is false. Alternatives are lf, crlf and native, which uses the platform’s native line ending. The default value isnative
. See gitattributes[5] for more information on end-of-line conversion.
當(dāng)core.autocrlf
是false
時(shí)恼五,設(shè)置行結(jié)束符的類型昌罩,可以是
lf
crlf
native
三種,其中native
是指平臺(tái)默認(rèn)的行結(jié)束符灾馒。默認(rèn)的類型是native
欲知詳情茎用,請(qǐng)翻閱gitattributes[5]或者mirrors.edge.kernel.org。
六你虹、總結(jié)一下吧
1. 不安全不保險(xiǎn)的方式
雖然Google查詢LF will be replaced by CRLF
绘搞,幾個(gè)答案會(huì)建議設(shè)置
git config --global core.autocrlf false
git config --global core.safecrlf false
比如stackoverflow彤避,知乎傅物,github上的bolg中的某些答案(這幾個(gè)鏈接里也是有一些干貨的),
但是琉预,通過(guò)上面我們看文檔的過(guò)程董饰,我們發(fā)現(xiàn)這種方式,是不保險(xiǎn)的圆米。很容易產(chǎn)生混合卒暂,git diff
的時(shí)候,就會(huì)影響我們查看版本之間的修改娄帖。
2. 個(gè)人推薦的設(shè)置方式
(1)添加.gitattributes
首先要在項(xiàng)目里添加.gitattributes文件也祠,可以參考Github help-Dealing with line endings和gitattributes[5],這是我自己的.gitattributes近速。
(2)safecrlf設(shè)置為true
git config --global core.safecrlf true
(3)autocrlf在不同平臺(tái)不同設(shè)置
- Windows
git config --global core.autocrlf true
- Mac Or Linux
git config --global core.autocrlf input
(4)eol默認(rèn)native诈嘿,因?yàn)閍utocrlf不是false,也不起作用啊
補(bǔ)充
第一點(diǎn) Git Setup Treat Line Endings In Text Files
Git默認(rèn)的core.autocrlf其實(shí)在安裝Git For Windows的時(shí)候削葱,讓我們選擇了奖亚,只是安裝的時(shí)候并不會(huì)注意,就是下面這張圖:
由上向下依次是
true input false
析砸。
第二點(diǎn) TortoiseGit settings about AutoCrlf
另外昔字,如果使用的是TortoiseGit,要注意其中的設(shè)置: