PricingSign inDownload

Blog / git

Agent edit shows every line changed? Line endings and whitespace

When an agent changes one word and git shows the whole file: how to see the real change, check for CRLF, and stop it with one .gitattributes line.

On this page
  1. See the real change
  2. Is it line endings?
  3. Fix it once for the project
  4. Find every file with the wrong endings
  5. What about core.autocrlf?
  6. Whitespace the agent added
  7. Keep the noise out of commits
  8. When teammates' agents share a project
  9. Frequently asked questions
  10. Why does git show the whole file as changed after a small edit?
  11. How do I stop CRLF line endings in a project?
  12. Does git diff -w change the file?
  13. What does i/lf w/crlf mean?
  14. Try it with your team

An agent changed one word in a file, and the diff shows every line removed and added again. The review is unreadable, and if it gets committed, git blame points at the agent for the whole file.

Almost always the agent's tool wrote the file back with different line endings (Windows CRLF instead of LF) or different whitespace. We reproduced both with git 2.43 on a scratch repo.

See the real change#

Our file had three lines. The agent changed line two to line 2 and saved it with CRLF endings:

bash
git diff --stat
 app.txt | 6 +++---

All three lines changed. Ignore the carriage returns and the real edit shows up:

bash
git diff --ignore-cr-at-eol
-line two
+line 2
 line three

For whitespace, two more flags:

  • git diff -w ignores all whitespace changes, including indentation and a missing newline at the end of the file.
  • git diff --ignore-space-at-eol ignores only trailing spaces, and still shows a tab that became spaces.

These only change what you see. The file on disk still has the noise.

Is it line endings?#

bash
git ls-files --eol app.txt
i/lf    w/crlf  attr/                 	app.txt

i/ is what git has stored, w/ is the file in your folder. i/lf with w/crlf means the agent switched the endings. file app.txt says the same in words: ASCII text, with CRLF line terminators.

Fix it once for the project#

Add one line to .gitattributes and commit it:

bash
printf '* text=auto eol=lf\n' > .gitattributes
git add .gitattributes
git add --renormalize .
git diff --cached --stat
warning: in the working copy of 'app.txt', CRLF will be replaced by LF the next time Git touches it
 .gitattributes | 1 +
 app.txt        | 2 +-

Git now stores LF whatever the agent writes, so the commit holds only the one real line. It works for every teammate and every agent on the project, which a personal core.autocrlf setting doesn't. If some files must keep CRLF (Windows .bat files, for example), add a line such as *.bat text eol=crlf after it.

If the project's files already mix endings, git add --renormalize . on its own commit fixes them all at once. Commit it apart from real work so the noise stays out of reviews.

Find every file with the wrong endings#

One file is easy to spot. To check the whole project, list every file whose stored or local copy has CRLF:

bash
git ls-files --eol | grep crlf
i/crlf  w/crlf  attr/                 	win.txt

i/crlf means the CRLF endings are already committed, not just in your folder. Those are the files git add --renormalize . changes. A file that shows w/crlf only is one an agent just rewrote; the .gitattributes line takes care of it on the next git add.

What about core.autocrlf?#

core.autocrlf is the older, per-person way to handle endings. It lives in each person's git config, so it only works for people who set it, and it does nothing for an agent running under a different account, in a container or on a teammate's computer. Check yours with:

bash
git config core.autocrlf

No output means it isn't set, which is the default on Mac and Linux. With a .gitattributes line in the project you don't need it, and the project's rule wins where both are set.

Whitespace the agent added#

Line endings aside, agents also add trailing spaces, swap tabs for spaces, or drop the newline at the end of a file:

-line three
+line three
\ No newline at end of file

The fix is the project's formatter. Run it before each commit, or ask the agent to, in your project instructions: "run the formatter on files you changed before committing". An .editorconfig file with end_of_line = lf, insert_final_newline = true and trim_trailing_whitespace = true also helps, for editors and tools that read it.

Keep the noise out of commits#

A diff that touches every line is easy to approve without reading, and that is where a real change hides. Before you commit an agent's work, compare git diff --stat with git diff -w --stat. If the numbers are far apart, fix the endings or whitespace first, then look at what's left. Our guide to reviewing PRs from several agents covers the rest of a review.

When teammates' agents share a project#

One person's agent on Windows and another's on a Mac is the usual way mixed endings get in. The .gitattributes line settles it for everyone. If you work with a teammate's agent on the same project, see working as a team with a coding agent each.

Frequently asked questions#

Why does git show the whole file as changed after a small edit?#

The file was saved with different line endings or whitespace. Run git diff --ignore-cr-at-eol or git diff -w to see the real change, and git ls-files --eol to check the endings.

How do I stop CRLF line endings in a project?#

Add "* text=auto eol=lf" to .gitattributes, commit it, and run git add --renormalize . once. Git then stores LF no matter how a file was saved.

Does git diff -w change the file?#

No. It only hides whitespace changes in the diff. The file keeps them until you fix it.

What does i/lf w/crlf mean?#

From git ls-files --eol: git has the file stored with LF endings, but the copy in your folder has CRLF.