In one of my git repository, I have added the following .gitattribute file to regulate LF/CRLF characters:
*.bat text eol=crlf
*.cmd text eol=crlf
*.java text eol=lf
*.scala text eol=lf
*.xml text eol=lf
*.py text eol=lf
*.R text eol=lf
# mimicking apache spark
To test its effect, I manually change the line separator of one bat file from CRLF to LF, unfortunately, when committing & pushing this change in git, it is accepted despite obviously violating the new .gitattribute:
How could it be possible? Is it a bug in git?
This is intentional from Git. By itself, it will never convert line endings to
CRLFwhen saving a file to its index. As soon as Git is told that a file extension is associated withtext, it will take every oportunity to convert the line endings toLFwhen you stage something to be committed with that extension.This can be explained by the fact that git was initilally developed for Unix which uses
LF. It's also a matter of consistency for git to deal with text in a uniform way internally when it comes to line endings.This is usually not a problem since any use of the code in a repository should start with a proper
git clonewhich performs the conversion according to the.gitattributes, so that line endings are correctly specified in the working directory.But in some special case where you really need to make git save line endings as
CRLF, you can have a look at this answer.