您需要将--keep-cr
标志传递给git am
. 这很不幸,但由于存在竞争标准(电子邮件工作流程与本地),确实没有太多选择。
您可能还想尝试设置.gitattributes
文件。尝试重新创建您的问题,当我将文件指定为需要 CRLF 时,我能够让事情正常工作。请注意,在没有首先规范化文件的情况下,它确实将整个文件显示为已修改。我通常使用 a .gitattributes
:
.gitattributes export-ignore
.gitignore export-ignore
*.txt text
*.C text trailing-space space-before-tab -indent-with-non-tab
*.rst text trailing-space space-before-tab -indent-with-non-tab
*.clj text trailing-space space-before-tab -indent-with-non-tab
*.c text diff=cpp trailing-space space-before-tab -indent-with-non-tab
*.cpp text diff=cpp trailing-space space-before-tab -indent-with-non-tab
*.h text diff=cpp trailing-space space-before-tab -indent-with-non-tab
*.hpp text diff=cpp trailing-space space-before-tab -indent-with-non-tab
*.py text diff=python trailing-space space-before-tab -indent-with-non-tab
*.tex text diff=tex
*.java text diff=java trailing-space space-before-tab -indent-with-non-tab
*.pl text diff=perl trailing-space space-before-tab -indent-with-non-tab
*.php text diff=php
*.rb text diff=ruby trailing-space space-before-tab -indent-with-non-tab
*.vcproj eol=crlf
*.dsp eol=crlf
*.dsw eol=crlf
*.sh eol=lf
*.jpg binary
*.png binary
*.gif binary
*.tiff binary
您需要根据gitattributes 手册页规范您的行尾。另一个 SO 用户最终也关闭了core.autocrlf
以获取干净的提交和补丁。
试图重现你的错误
$ git init repo
Initialized empty Git repository in c:/tmp/git-eol/repo/.git/
$ cd repo
$ git config --local core.autocrlf false
$ vim foo.txt
$ git add foo.txt
$ git commit -m "Add foo."
[master (root-commit) 3903abd] Add foo.
1 file changed, 3 insertions(+)
create mode 100644 foo.txt
$ vim foo.txt
$ git st
## master
M foo.txt
$ git commit -m "Add more foo." -a
[master 03e991a] Add more foo.
1 file changed, 2 insertions(+)
$ git format-patch HEAD~1
0001-Add-more-foo.patch
$ vim 0001-Add-more-foo.patch
查看生成的补丁文件,我看到:
如您所见,回车符 ( ^M
s) 保留在补丁中。这是与core.autocrlf=false
. 继续,我看到:
$ git reset --hard HEAD~1
HEAD is now at 3903abd Add foo.
$ git am 0001-Add-more-foo.patch
Applying: Add more foo.
error: patch failed: foo.txt:1
error: foo.txt: patch does not apply
Patch failed at 0001 Add more foo.
The copy of the patch that failed is found in:
c:/tmp/git-eol/repo/.git/rebase-apply/patch
When you have resolved this problem, run "git am --resolved".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
$ git am --abort
所以补丁不能很好地开箱即用。使用--keep-cr
符合预期:
$ git am --keep-cr 0001-Add-more-foo.patch
Applying: Add more foo.
$
好的。因此,让我们尝试一下core.autocrlf=true
(在不同的存储库中):
# Removed the initial commands...
$ git format-patch HEAD~1
0001-Add-more-foo.patch
$ git reset --hard HEAD~1
HEAD is now at 525b5aa Initial commit.
$ git am 0001-Add-more-foo.patch
Applying: Add more foo.
$ git config --get core.autocrlf
true
在这种情况下,补丁到处都有 LF 结尾。