A small edit can appear to replace an entire file when one tool writes CRLF endings and another writes LF. The resulting diff obscures the behavior under review and can leave scripts with bytes their runtime does not expect. Put the intended line-ending policy in the repository and verify both stored and checked-out content.
This guide covers ordinary text normalization through .gitattributes, including a bounded migration of tracked files. Commands were exercised with Git 2.45.2 on Windows and use PowerShell-compatible and Bash-compatible syntax. The example filenames describe a synthetic repository, not a policy to copy into every project.
Inspect the index separately from the working tree
Start from a clean checkout or a dedicated migration branch with no unrelated staged work. Preserve unfinished changes before applying normalization. An apparently noisy diff may contain meaningful edits that become difficult to distinguish once conversion is staged.
Inspect a small set of representative tracked paths:
git status --short
git ls-files --eol -- src/example.ts scripts/check.sh scripts/build.cmd
git check-attr text eol -- src/example.ts scripts/check.sh scripts/build.cmd
The ls-files manual documents the i/ index and w/ working-tree line-ending indicators and the effective text attribute. An index value of LF alongside a working-tree value of CRLF can be an intended conversion rather than an uncommitted change.
Use check-attr to see which attributes apply to each path. A nested attributes file or local override can explain why two neighboring files behave differently. Inspect the relevant rules before changing machine-wide Git settings.
Also inspect the editor and formatter configuration through the project's normal review process. Those tools write working-tree files before Git sees them. Repository attributes help define Git's boundary; agreement among editors, formatters, and runtimes prevents repeated conversion while a file is being edited.
Choose rules from the files' consumers
For a synthetic project that requires LF source and shell scripts, CRLF command scripts, and unchanged PNG bytes, an attributes file could contain:
*.ts text eol=lf
*.sh text eol=lf
*.cmd text eol=crlf
*.png -text
The attributes documentation distinguishes text normalization in the repository from the eol setting used for the working tree. With these text rules, normalized repository content uses LF; the command script can still be checked out with CRLF. The unset text attribute excludes PNG files from this conversion.
Choose each rule because a runtime, tool, or reviewed project convention requires it. Do not assume that every Windows consumer requires CRLF or that all executable scripts share one requirement. Inspect generated files, vendored content, fixtures, and formats with embedded signatures separately.
Line-ending normalization does not provide a general character-encoding conversion policy. A UTF-16 fixture or byte-sensitive protocol sample needs deliberate handling. Keep such files outside a broad text rule until their expected bytes and conversion behavior have been checked.
If using automatic text detection for the rest of the repository, first inventory exceptional paths. Automatic classification is convenient, but the migration still needs evidence that binary data and intentionally mixed-ending fixtures retain their meaning.
Normalize a reviewed scope and inspect what was staged
Add the policy file explicitly, then reapply normalization only to the tracked paths selected for the migration:
git add -- .gitattributes
git add --renormalize -- src/example.ts scripts/check.sh scripts/build.cmd
git diff --cached --stat
git diff --cached
The Git add manual explains that --renormalize reapplies clean processing to tracked files. That can include configured clean filters, so review those filters before treating the operation as line-ending conversion alone. The explicit path list also keeps unrelated tracked content outside this example.
Review every staged file, including the attributes file. Confirm that filenames, permissions, and content remain expected. If ordinary edits were present before normalization, separate them before proceeding; otherwise the migration commit will carry behavior changes behind a large byte-level diff.
A broader migration may be justified after the representative files pass. Expand through known directories and inspect each resulting batch. Avoid staging the whole working tree merely because a conversion command appears routine.
Use whitespace comparisons as a diagnostic aid
A supplemental comparison can make a conversion-only patch easier to inspect:
git diff --cached --ignore-space-at-eol
git ls-files --eol -- src/example.ts scripts/check.sh scripts/build.cmd
The diff manual documents ignoring whitespace at line endings. This hides more than carriage returns, so an empty comparison is insufficient evidence that no meaningful bytes changed. Review the ordinary staged diff and any format-specific validation as well.
For byte-sensitive fixtures, compare the expected bytes directly in an isolated test. For scripts, execute the actual entry point under the intended interpreter. A formatter completing successfully does not establish that a deployment script or generated checksum remains valid.
Keep normalization in a dedicated commit with a precise message. That gives future reviewers a clear explanation for the broad diff and lets a later logic change receive ordinary scrutiny. Use the focused staging workflow when the surrounding work needs its own commit boundaries.
Verify new checkouts on supported platforms
An existing working tree may retain its previous bytes until files are refreshed. Verify the policy through a fresh disposable checkout after committing the reviewed migration. Check the same representative paths with ls-files --eol and run their real consumers.
Exercise the supported development and CI environments. Confirm that another edit-and-stage cycle does not produce an unexplained whole-file replacement. If it does, identify whether the writer, attributes, filters, or generated outputs caused the difference before adding more global settings.
If the migration changes behavior, retain the evidence and correct the affected rule before expanding its scope. Recovery should preserve any legitimate edits made after the migration. A useful policy makes both the stored representation and each required working-tree representation predictable, with exceptions explained by the files that need them.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit