我注意到,當我在我的 git 存盤庫中有一個帶有 CRLF 行結尾的檔案時,我嘗試創建并應用一個更改該檔案的補丁,但它失敗了。
這是重現問題的簡單方法:
$ git init
$ echo -en "foo\r\nbar\r\n" > a.txt
$ git add a.txt
$ git commit -m 'first commit'
$ echo -en "abcd\r\n" >> a.txt
$ git commit -a -m 'second commit'
$ git format-patch -1 HEAD
$ git checkout HEAD~
$ git am 0001-second-commit.patch
Applying: second commit
error: patch failed: a.txt:1
error: a.txt: patch does not apply
Patch failed at 0001 second commit
hint: Use 'git am --show-current-patch' to see the failed patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
是什么導致了這個問題?我怎樣才能避免它?
uj5u.com熱心網友回復:
TL;DR:使用git am --keep-cr.
補丁本身,在0001-second-commit.patch,實際上說:期望讀取最后一行bar加上一個回車;在此之后添加另一行,也帶有回車符git am,但在郵箱上使用的“郵件拆分”程序會洗掉兩個回車符。因此,此時有效運行的內部git apply步驟是:哇,等等,等一下!這里的原文不符!我最好失敗,讓人類幫忙。git am
要解決此問題,請告知git am以不同方式呼叫郵件拆分程序。默認情況下,git mailsplit將行尾的回車視為某些電子郵件軟體所犯的錯誤,并將其洗掉。該--keep-cr選項git mailsplit表明不,這不是一個錯誤:請保持那些馬車轉彎。此選項本身也提供git am,并且git am(但不是)有一個配置旋鈕可以git mailsplit默認打開它:可以設定為.am.keepcrtrue
如果您已配置am.keepcr并true希望暫時覆寫它,git am也--no-keep-cr可以。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/441095.html
標籤:混帐
