如何让git拒绝任何eol样式更改? [英] How to let git reject any eol style change?

查看:209
本文介绍了如何让git拒绝任何eol样式更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时人们在提交代码时无意中改变了eol风格。如何让git阻止他们这样做?我搜索了这个话题,发现大多数答案都集中在如何将文件转换成特定的eol风格。但我不想要这种行为。我只是希望提交者遵守原始文件的eol风格,无论是\ n还是\ r \ n。可以这样做吗?

解决方案


我只需要强制提交符合原始eol样式作者选择。也就是说,既不能将\\\
转换为\ r \ n,也不能将\r \\\
转换为\\\
。但我不在乎最初的eol风格是什么


如果您有 core.autocrlf 设置为 false ,git不会尝试在任何时间转换任何东西。 但这也意味着用户编辑器可以在没有Git的情况下更改eol:Git在结帐时不会记住eol。

这意味着它将检查用户修改的任何内容(包括eol更改)。



您可以尝试 结账后挂钩,它将负责为您记忆该信息,但是看起来有点矫枉过正。

我喜欢为 .gitattributes 文件中的某些类型的文件注册标准化,因为本答案的下一部分说明了这一点。 / p>




原始答案,说明Git可以在提交时执行



您可以在注册所需的文件(用于特定类型的文件) .gitattributes 文件,带有eol指令:

  text 




此属性启用并控制end-
当文本文件被标准化时,其行结束符会在存储库中转换为LF。

要控制工作目录中使用的行结束样式, 对单个文件使用 eol 属性,对所有文本文件使用 core.eol 配置变量




  eol 




这个属性在工作目录中设置了一个特定的行结束样式。

它使得end-如果没有任何内容检查,就可以在线标准化,从而有效地设置文本属性。



设置为字符串值 crlf

这个设置强制Git在签入时对这个文件的行结束进行规范化处理,并且在文件被转换时将它们转换为 CRLF 签出。



设置为字符串值 lf



在签入时将行尾标准化为 LF ,并防止在签出文件时转换为 CRLF


例如:

  *。sh eol = lf 

将这些指令注册到 .gitattributes 文件是它将在整个存储库克隆中持续存在


Sometimes people inadvertently change the eol style when committing codes. How to let git stop them from doing that? I've searched this topic and found out that most answers focused on how to turn files into a specific eol style. But I don't want this behavior. I just want committers to comply with the eol style of the original files, either \n or \r\n. Can git do that?

解决方案

I just need to force committers to comply with the original eol style that the author has chosen. That is to say, neither converting \n to \r\n, nor converting \r\n to \n is allowed. But I don't care what the original eol style is

If you have core.autocrlf set to false, git won't try to convert anything at anytime.

But that also means the user editor can change the eol without Git knowing anything about it: Git doesn't memorize the eol during checkout.
That means that it will checkin whatever has been modified by the user (including eol changes).

You could try a post-checkout hook which would be in charge of memorizing that information for you, but that seems a bit overkill.
I prefer registering a normalization for certain types of files in .gitattributes files, as the next section of this answer illustrates.


Orginal Answer, illustrating what Git can enforce at commit time:

You can register the eol you want (for specific types of files) in .gitattributes files, with eol directives:

text

This attribute enables and controls end-of-line normalization.
When a text file is normalized, its line endings are converted to LF in the repository.
To control what line ending style is used in the working directory, use the eol attribute for a single file and the core.eol configuration variable for all text files.

eol

This attribute sets a specific line-ending style to be used in the working directory.
It enables end-of-line normalization without any content checks, effectively setting the text attribute.

Set to string value "crlf"

This setting forces Git to normalize line endings for this file on checkin and convert them to CRLF when the file is checked out.

Set to string value "lf"

This setting forces Git to normalize line endings to LF on checkin and prevents conversion to CRLF when the file is checked out.

For instance:

*.sh        eol=lf

The interest in registering those directives in a .gitattributes file is that it will be persistent across repository cloning.

这篇关于如何让git拒绝任何eol样式更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆