🔀 Fix a .gitattributes Line Ending Rule That Only Takes Effect on Files Checked Out After It Was Added
Adding a line ending rule to .gitattributes changes how Git will normalize files during future checkout and checkin operations – it does not reach backward and rewrite the line endings already sitting in files that are already committed and already present in your working tree. A file added to the repository before the rule existed keeps whatever line endings it already had until something actually causes Git to check it out again from scratch, which a simple edit to .gitattributes alone will never trigger on its own.
🔎 The Problem
$ cat .gitattributes *.sh text eol=lf $ git add .gitattributes $ git commit -m "Normalize shell script line endings" $ file scripts/deploy.sh scripts/deploy.sh: ASCII text, with CRLF line terminators # still CRLF - this file was committed before the rule # existed and has never actually been re-checked-out since
✅ Fix: Force a Renormalization After Changing .gitattributes
- After adding or changing a line ending rule, explicitly renormalize every already-tracked file against the new rule using the renormalize option on git add, rather than assuming the rule alone will eventually apply on its own.
- Commit the resulting line ending changes as their own dedicated commit, separate from any unrelated feature work, so the renormalization doesn’t get buried inside an otherwise unrelated diff.
- Warn collaborators before they pull a renormalization commit, since it can touch a large number of files purely due to line endings and is easy to mistake for unrelated, sweeping changes if nobody was told it was coming.
🚨 Why This Is Dangerous
- A renormalization commit that touches many files at once can look alarming in review or in a diff tool if its purpose isn’t clearly labeled – state plainly in the commit message that it’s a pure line-ending normalization and nothing else changed.
.gitattributes describes how Git should treat files going forward – it was never going to reach back in time and fix the line endings already sitting in your history.
