Sub topic 1
Why Line Endings Differ and Why Git Cares
Prerequisites
This chapter assumes you can clone a repository, stage and commit changes, and read the output of git status and git diff. You should be comfortable in a terminal. You should also know that a file is stored as bytes and that a text editor interprets those bytes as characters.
Two ways to end a line
A text file has no real "lines" on disk. It is a sequence of bytes, and some of those bytes mark where one line stops and the next begins. Two conventions are in common use:
- LF (line feed, byte
0x0A, written\n) is used by Linux, macOS and other Unix-like systems. - CRLF (carriage return followed by line feed, bytes
0x0D 0x0A, written\r\n) is the Windows convention.
The split dates back to typewriters and teleprinters. A carriage return moved the print head to the start of the line, and a line feed advanced the paper. Windows kept both bytes. Unix kept only the line feed. Old classic Mac OS used a lone CR, but you will rarely meet that today.
Most modern editors read either style. The problem starts when a file moves between people who use different styles and the tools in between treat the extra byte as content.
Seeing the difference
You can check the bytes yourself. The printf command below writes the same two lines with each ending, and od -c prints every byte.
printf 'hello\nworld\n' > unix.txt
printf 'hello\r\nworld\r\n' > windows.txt
od -c unix.txt
od -c windows.txtThe first file shows \n after each word. The second shows \r \n. The files look identical in most editors, but they differ by two bytes, and Git compares bytes.
Why Git cares
Git stores file content exactly as given unless you tell it otherwise. It does not know that \r\n and \n mean the same thing to a human. That causes several concrete problems.
- Noisy diffs. If a teammate on Windows opens a file and their editor rewrites every line ending as CRLF,
git diffreports every line as changed, even though no text changed. - Hard merges. A branch with CRLF lines and a branch with LF lines conflict on lines that look identical, because the lines differ in their last byte.
- Broken scripts. A shell script with CRLF endings fails on Linux. The kernel reads the first line as
#!/bin/sh\rand looks for an interpreter with a carriage return in its name. You see an error such as/bin/sh^M: bad interpreter. - Unreliable blame and history. A whole-file ending change makes
git blamepoint at the person who converted the file instead of the person who wrote each line. - Corrupted binaries. If a tool converts line endings in a file that is not text, such as an image, it changes bytes that were never line endings. The file may no longer open.
Git can warn you about the first case. After you stage a file whose endings are about to change, you may see this message:
warning: in the working copy of 'run.sh', LF will be replaced by CRLF the next time Git touches itThe message means a conversion setting is active. It tells you Git will change bytes between the repository and your working tree.
Where conversion happens
Git has two places where a file lives: the repository (the committed content, also called the index when staged) and the working tree (the files you edit). Line-ending conversion happens when content crosses between them.
The common goal is simple. The repository should hold one style, normally LF, so diffs and merges stay clean. The working tree should hold whatever style each tool and platform needs. The rest of this tutorial covers the settings that control those two conversions: first per-user configuration, then a shared .gitattributes file.
Exercises
Exercise
A Linux user runs a shell script that a Windows colleague committed. It fails with /bin/sh^M: bad interpreter. What is in the file, and why does it cause this error?
Show answer
The file has CRLF line endings. The first line is #!/bin/sh followed by \r\n. Linux treats only \n as the line end, so the \r becomes part of the interpreter name, /bin/sh\r, which does not exist.
Exercise
The file notes.txt contains the text hi and yo on two lines, each ending with CRLF. How many bytes is it, and how many would it be with LF endings?
Show answer
With CRLF it is 8 bytes (hi 2 + 2, yo 2 + 2). With LF it is 6 bytes (hi 2 + 1, yo 2 + 1).