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
| Change | What happened | Typical cause |
|---|---|---|
| Line endings | LF (\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 whitespace | Spaces or tabs added or removed at the end of a line. | Editor settings, copy and paste from a web page. |
| Indentation | Tabs became spaces, or the width changed from 2 to 4. | A formatter or a different editor setup. |
| Final newline | The last line lost or gained its line break. | Editors disagree about ending a file with a newline. |
| Byte order mark | A UTF-8 BOM (EF BB BF) was added, so the first line differs. | Some Windows tools save UTF-8 with a BOM. |
| Look alike spaces | A 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 diff --checklists trailing whitespace, including a carriage return at the end of a line.cat -A file.txtshows^Mfor carriage returns,^Ifor tabs and$at each line end.file config.ymlreportswith CRLF line terminatorsfor Windows line endings.- VS Code shows
LForCRLFin the status bar. Click it to convert. Theeditor.renderWhitespacesetting shows spaces and tabs.
Git options for ignoring whitespace
These options change what git displays, not the files. The whitespace change is still in the commit.
| Option | What it ignores |
|---|---|
--ignore-cr-at-eol | Only a carriage return at the end of a line. The YAML example shows no changes with it. |
--ignore-space-at-eol | Whitespace changes at the end of lines. |
-b | Changes in the amount of whitespace. One space equals four, but no space does not equal one. |
-w | All whitespace. a=b and a = b count as equal. |
--ignore-blank-lines | Changes 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
- Python and YAML use indentation as structure. A changed indent can move a line out of a loop.
- Makefiles fail if a tab becomes spaces.
- Shell scripts with CRLF fail on Linux with
/bin/bash^M: bad interpreter. - Heredocs, multi-line strings and fixed width data treat every space as content.
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