Git CRLF Normalization with .gitattributes¶
概念概覽
處理流程¶
核心知識¶
處理流程¶
分兩個獨立 commit:
1. 第一個 commit:新增/修改 .gitattributes,宣告 * text=auto 或針對特定副檔名設定 eol=lf
2. 第二個 commit:執行 git add --renormalize . 讓 Git 依新規則重新正規化所有已追蹤檔案,提交結果
為什麼要分兩個 commit¶
.gitattributes 變更本身是配置決策,renormalize 後的大量檔案變更是機械性結果。混在一起難以 code review,也難以 bisect。
常見陷阱¶
若只加 .gitattributes 而不執行 renormalize,已存在的 CRLF 檔案不會被自動修正,噪音依然存在。
經驗教訓¶
-
CRLF 正規化必須 .gitattributes + renormalize 兩步缺一不可
-
分兩個 commit 能讓 git blame / bisect 更清晰
常見陷阱¶
-
只加 .gitattributes 不執行 renormalize,既有檔案的 CRLF 不會被清除
-
renormalize 後的 diff 很大,容易掩蓋真正的邏輯變更,務必獨立一個 commit
相關概念¶
來源 Sessions¶
| 日期 | Session | 貢獻摘要 |
|---|---|---|
| 2026-04-11 | 38030a5c-54da-4873-9a63-680709c843f1 | 透過 .gitattributes + git renormalize 清除專案中的 CRLF 噪音,分兩個 commit 處理配置與實際正規化 |