Whitespace and line ending changes in diffs

When a diff marks lines as changed but they look identical, the difference is in characters you cannot see. Here is how to find them, hide them during review, and stop them coming back.

The symptom

A one line fix shows the whole file as replaced. In this real case a small YAML file was saved by an editor that uses Windows line endings, and git diff marks all three lines:

@@ -1,3 +1,3 @@-server:-  port: 8080-  host: localhost+server:+  port: 8080+  host: localhost

Piping the same output through cat -A, which prints hidden characters, shows the real change. Each new line ends with ^M, the carriage return:

-server:$
-  port: 8080$
-  host: localhost$
+server:^M$
+  port: 8080^M$
+  host: localhost^M$

The invisible changes to look for

ChangeWhat happenedTypical cause
Line endingsLF (\n) became CRLF (\r\n) or the reverse.A Windows editor, a file sent by email, or a git setting that converts on checkout.
Trailing whitespaceSpaces or tabs added or removed at the end of a line.Editor settings, copy and paste from a web page.
IndentationTabs became spaces, or the width changed from 2 to 4.A formatter or a different editor setup.
Final newlineThe last line lost or gained its line break.Editors disagree about ending a file with a newline.
Byte order markA UTF-8 BOM (EF BB BF) was added, so the first line differs.Some Windows tools save UTF-8 with a BOM.
Look alike spacesA space became a non breaking space (U+00A0) or a zero width character.Text pasted from a word processor or a PDF.

How to see what changed

Git options for ignoring whitespace

These options change what git displays, not the files. The whitespace change is still in the commit.

OptionWhat it ignores
--ignore-cr-at-eolOnly a carriage return at the end of a line. The YAML example shows no changes with it.
--ignore-space-at-eolWhitespace changes at the end of lines.
-bChanges in the amount of whitespace. One space equals four, but no space does not equal one.
-wAll whitespace. a=b and a = b count as equal.
--ignore-blank-linesChanges where the only added or removed lines are blank.

Start with the narrowest option that removes the noise. Compare Codes works differently: it always normalizes line endings, so the YAML change above shows nothing there at all. Use git or cat -A when you need to see line endings. Its Ignore whitespace option sits between -b and -w.

Fix it at the source with .gitattributes

Tell git how line endings are stored, in a file committed with the project:

# Store text files with LF in the repository
* text=auto

# Shell scripts must keep LF, even on Windows
*.sh  text eol=lf

# Windows batch files need CRLF
*.bat text eol=crlf
*.cmd text eol=crlf

# Never convert binary files
*.png binary
*.jpg binary
*.zip binary

Then normalize the files already in the repository, in a commit of its own so reviewers can skip it:

git add --renormalize .
git commit -m "Normalize line endings"

The core.autocrlf setting also controls conversion, but it is set per computer and easy to get wrong. A .gitattributes file applies to everyone who clones the project.

Prevent it with .editorconfig

Most editors read .editorconfig and follow it when saving:

root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab

Markdown is an exception because two trailing spaces create a line break. Makefiles need tabs at the start of recipe lines.

When whitespace is not noise

For these files, review once with whitespace visible before you accept a change.

Hide formatting commits from git blame

After a large reformat, git blame points every line at that commit. List its hash in .git-blame-ignore-revs and run the line below. GitHub reads the same file.

git config blame.ignoreRevsFile .git-blame-ignore-revs