简体   繁体   English

“git checkout --patch”是否与 autocrlf=true 不兼容?

[英]Is "git checkout --patch" incompatible with autocrlf=true?

On my windows machine I'm considering setting autocrlf = true in my .gitconfig.在我的 Windows 机器上,我正在考虑在我的 .gitconfig 中设置autocrlf = true Others on my team have it set this way and we've ended up with a mix of lf & crlf in the repo.我团队中的其他人是这样设置的,我们最终在 repo 中混合了lfcrlf I've considered a few options and this seems like the path of least resistance...我已经考虑了一些选择,这似乎是阻力最小的路径......

One thing I've noticed is that git checkout -p fails with the error patch does not apply when autocrlf = true .我注意到的一件事是git checkout -p失败,错误patch does not applyautocrlf = truepatch does not apply If I change it back to false it works as expected.如果我将其改回false它将按预期工作。

Is autcrlf = true incompatible with checkout --patch or is do I need to set some other option to get it to work? autcrlf = true是否与checkout --patch不兼容,还是我需要设置一些其他选项才能使其工作?

Interestingly git add --patch works as expected with autocrlf = true .有趣的是git add --patchautocrlf = true一起按预期工作。

Yes , git checkout --patch works properly with line ending translation.是的git checkout --patch与行尾翻译正常工作。 The patch almost certainly isn't applying because you have mismatched configuration.该补丁几乎肯定不会应用,因为您的配置不匹配。

Everybody on your team needs to use the same line ending configuration.您团队中的每个人都需要使用相同的行尾配置。 core.autocrlf=true isn't an option that one person on a team can enable; core.autocrlf=true不是团队中的一个人可以启用的选项; everybody needs the same settings.每个人都需要相同的设置。 That's because if you have core.autocrlf=false , then you're telling Git that the contents on disk are identical to the contents in the repository.那是因为如果您有core.autocrlf=false ,那么您就是在告诉 Git 磁盘上的内容与存储库中的内容相同。 If you're on Windows, that means that you're checking in files with Windows-style line endings ( \\r\\n ).如果您使用的是 Windows,则意味着您正在签入带有 Windows 样式行结尾 ( \\r\\n ) 的文件。 If you have a colleague who enables core.autocrlf=true , then you're telling Git that the contents on disk have Windows-style line endings while the contents in the repository have Unix style line endings ( \\n ).如果您的同事启用了core.autocrlf=true ,那么您就是在告诉 Git,磁盘上的内容具有 Windows 样式的行尾,而存储库中的内容具有 Unix 样式的行尾 ( \\n )。

But your colleague has told git a lie, since the repository contents actually have \\r\\n in them.但是您的同事对 git 撒了谎,因为存储库内容实际上包含\\r\\n

A variety of strange things can occur when these settings are mismatched, including things like patches failing to apply.当这些设置不匹配时,可能会发生各种奇怪的事情,包括无法应用补丁之类的事情。

You should instead:你应该:

  1. Set * text=auto in your .gitattributes file..gitattributes文件中设置* text=auto This ensures that everybody working in the repository has the same settings, since the configuration is actually checked in and not something that everybody has to remember to configure properly.这确保在存储库中工作的每个人都具有相同的设置,因为配置实际上是签入的,而不是每个人都必须记住正确配置的内容。

  2. Renormalize the repository, so that the repository contents actually match what you've claimed.重新规范存储库,以便存储库内容实际上与您声明的内容相匹配。 You can do this by git add --renormalize .你可以通过git add --renormalize .来做到这git add --renormalize . and checking the contents in.并检查内容。

Once you've done this (and everybody has updated your local working folders), your git apply --patch should behave correctly.完成此操作后(并且每个人都更新了您的本地工作文件夹),您的git apply --patch应该可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM