How to tell git to preserve CRLF specifically for a single set of files? Mark a text file as binary?

送分小仙女□ 提交于 2020-08-10 19:45:13

问题


I want to set some of the text files in a repo to be cloned/downloaded as binary. Ie: Bypass the CRLF settings in git and just clone byte per byte.

Context

There are myriads of posts about CRLF in git. But normally they are about the general settings of whole projects, mainly depending on if you use Linux or Windows.

But I face a situation that never found before. I am willing to set a folder in my project with sample emails as defined by the RFC 5322 which define that the end-of-line of the emails are exactly CRLF. It's a project related to emailing and those are going to be used for unit-testing.

If I leave the system to treat the text files as it wants, those files will download as LF in many cases.

I want those few files to be "forced" to be downloaded in "binary mode", so whatever the platform or settings where you are doing the git clone, those files will not have lost a single byte; otherwise they would break the standard.

Question

  • Is this possible?
  • How can I signal only those those files as "binary" at the add or commit time so anyone willing to clone will have them properly cloned?
  • Is there any switch for the add or commit I should use?

回答1:


You can indeed mark these files as binary in your .gitattributes file, but that will prevent them from being diffed and merged, which may not be what you're after. The easier way is to specify them as having CRLF line endings, like so:

*.mbox eol=crlf

That will force those files to be written into the working tree with CRLF endings regardless of platform. If you did, for some reason, want to mark them as binary, you could replace the eol=crlf with binary. You might want that if your emails contained raw 8bit unencoded data, for example.



来源:https://stackoverflow.com/questions/61845148/how-to-tell-git-to-preserve-crlf-specifically-for-a-single-set-of-files-mark-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!