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 theeol
attribute for a single file and thecore.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 toCRLF
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.
来源:https://stackoverflow.com/questions/19261912/how-to-let-git-reject-any-eol-style-change