git line break LF and CRLF conversion problem

1. Background

Under each operating system, the newline characters used in text files are different. UNIX/Linux uses 0x0A (LF), early Mac OS uses 0x0D (CR), and later OS X is consistent with UNIX after changing the kernel. But DOS/Windows has always used 0x0D0A (CRLF) as a newline. Git provides a "newline automatic conversion" feature. This feature is in "auto mode" by default, when you are checking out a file, it tries to replace UNIX newlines (LF) with Windows newlines (CRLF); when you commit files, it tries to replace CRLF with LF. Git's "automatic newline conversion" feature sounds smart and sweet, as it tries to keep files in the repository consistent (UNIX-style) and compatible with local files (Windows-style). Unfortunately, this feature is buggy and unlikely to be fixed anytime soon.

2. Solutions

1. Git settings

git config --global core.autocrlf false
git config --global core.safecrlf true
Meaning:
AutoCRLF #Convert
to LF when submitting, and convert to CRLF when checking out
git config --global core.autocrlf true

#Convert to LF when submitting, not convert when checking out
git config --global core.autocrlf input

#Commit checkout does not convert
git config --global core.autocrlf false
SafeCRLF #Refuse
to submit files containing mixed newlines
git config --global core.safecrlf true

#Allow submission of files containing mixed newlines
git config --global core.safecrlf false

#Give a warning when submitting a file containing mixed newlines
git config --global core.safecrlf warn

2. IDE settings use UNIX line breaks

IDEA's settings File -> Settings
Editor -> Code Style
Line separator (for new lines), select: Unix and OS X (\n)

For files that have used Windows line breaks, you can use Sublime Text to open them,
View->Line Endings, select Unix, and save;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326158670&siteId=291194637