通過(guò)閱讀git-config文檔理解Git如何使用autocrlf帖蔓、safecrlf矮瘟、eol和.gitattributes處理line-ending

[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 of core.autocrlf, Git will reject the file.

簡(jiǎn)單翻譯一下:當(dāng)core.autocrlftrue或者input時(shí)磅崭,算激活了eol,此時(shí)如果core.safecrlftrue瓦哎,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.autocrlftrue的要求疆股。當(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.safecrlfwarn崔赌,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í)有LFCRLF的文件是被腐蝕的,這類文件是不被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 is native. See gitattributes[5] for more information on end-of-line conversion.
當(dāng)core.autocrlffalse時(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 endingsgitattributes[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ì)注意,就是下面這張圖:

Git treat line endings in text files

由上向下依次是true input false析砸。

第二點(diǎn) TortoiseGit settings about AutoCrlf

另外昔字,如果使用的是TortoiseGit,要注意其中的設(shè)置:


TortoiseGit settings
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末首繁,一起剝皮案震驚了整個(gè)濱河市作郭,隨后出現(xiàn)的幾起案子陨囊,更是在濱河造成了極大的恐慌,老刑警劉巖夹攒,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件谆扎,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡芹助,警方通過(guò)查閱死者的電腦和手機(jī)堂湖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)状土,“玉大人无蜂,你說(shuō)我怎么就攤上這事∶晌剑” “怎么了斥季?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)累驮。 經(jīng)常有香客問(wèn)我酣倾,道長(zhǎng),這世上最難降的妖魔是什么谤专? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任躁锡,我火速辦了婚禮,結(jié)果婚禮上置侍,老公的妹妹穿的比我還像新娘映之。我一直安慰自己,他們只是感情好蜡坊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布杠输。 她就那樣靜靜地躺著,像睡著了一般秕衙。 火紅的嫁衣襯著肌膚如雪蠢甲。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,190評(píng)論 1 299
  • 那天据忘,我揣著相機(jī)與錄音鹦牛,去河邊找鬼。 笑死若河,一個(gè)胖子當(dāng)著我的面吹牛能岩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播萧福,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼拉鹃,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起膏燕,我...
    開(kāi)封第一講書(shū)人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤钥屈,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后坝辫,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體篷就,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年近忙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了竭业。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡及舍,死狀恐怖未辆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情锯玛,我是刑警寧澤咐柜,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站攘残,受9級(jí)特大地震影響拙友,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜歼郭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一遗契、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧实撒,春花似錦姊途、人聲如沸涉瘾。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)立叛。三九已至负敏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間秘蛇,已是汗流浹背其做。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留赁还,地道東北人妖泄。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像艘策,于是被迫代替她去往敵國(guó)和親蹈胡。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • git 使用筆記 git原理: 文件(blob)對(duì)象,樹(shù)(tree)對(duì)象罚渐,提交(commit)對(duì)象 tree對(duì)象 ...
    神刀閱讀 3,771評(píng)論 0 10
  • 良好的配置有助于提高效率,減少不必要的錯(cuò)誤源织。把運(yùn)行配置的相關(guān)內(nèi)容單拎出來(lái)放到這里翩伪,查找和更新方便。 Customi...
    michael_jia閱讀 20,763評(píng)論 1 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,476評(píng)論 0 23
  • 日下成影 逆風(fēng)獨(dú)行 對(duì)月當(dāng)空 凝思頓挫 驚霎之間 人生幾何 不知方圓 如若地方 路間設(shè)坷 雖有險(xiǎn)峻 但求華麗 如若...
    虹帆閱讀 400評(píng)論 0 8
  • 第一:在不能了解客戶的真實(shí)問(wèn)題時(shí)谈息,盡量讓客戶說(shuō)話 多打聽(tīng)一些問(wèn)題幻工,帶著一種好奇的心態(tài),發(fā)揮刨根問(wèn)底的精神黎茎,讓客戶多...
    思念的詩(shī)閱讀 174評(píng)論 0 0