Line endings also called newline, end of line (EOL) or line break is a control character or sequence of control characters in a character encoding specification (e.g. ASCII or EBCDIC) that is used to signify the end of a line of text and the start of a new one. Some text editors set/implement this special character when you press the Enter key.
The Carriage Return, Line Feed characters are ASCII representations for the end of a line (EOL). They will end the current line of a string, and start a new one.
However, at the operating system level, they are treated differently:
The Carriage Return ("CR") character (ASCII 13\0x0D, \r): Moves the cursor to the beginning of the line without advancing to the next line. This character is used as the new line character in Commodore and Early Macintosh operating systems (Mac OS 9 and earlier).
The Line Feed ("LF") character (ASCII 10\0x0A, \n): Moves the cursor down to the next line without returning to the beginning of the line. This character is used as the new line character in Unix based systems (Linux, macOS X, Android, etc).
The Carriage Return Line Feed ("CRLF") character (0x0D0A, \r\n): This is actually two ASCII characters and is a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line. This character is used as the new line character in most other non-Unix operating systems, including Microsoft Windows and Symbian OS.
Normalizing inconsistent line endings in Visual Studio means selecting one character type to be used for all your files. It could be:
- The Carriage Return Line Feed ("CRLF") character
- The Line Feed ("LF") character
- The Carriage Return ("CR") character
However, you can set this in a better way using .gitattributes
file in your root directory to avoid conflicts when you move your files from one Operating system to the other.
Simply create a new file called .gitattributes
in the root directory of your application:
touch .gitattributes
And add the following in it:
# Enforce Unix newlines
* text=auto eol=lf
This enforces the Unix line feed line ending character.
Note: If this is an already existing project, simply run this command to update the files for the application using the newly defined line ending as specified in the .gitattributes
.
git rm --cached -r .
git reset --hard
That's all.
I hope this helps