window vscode修改代码后,保存后,git 工具查看发现全文差异,这是什么问题
·
1)第一嫌疑:CRLF ↔ LF 换行符被 VS Code 自动转换(最常见)
Windows 默认 CRLF,而很多仓库(尤其 Zephyr/Linux 风格)要求 LF。
当 VS Code 保存时把换行符全体转换,Git 就会认为每一行都变了 → 看起来像全文 diff。
✅ 怎么确认
- 打开文件,在 VS Code 右下角会显示
CRLF或LF - 或用 Git 看差异是否只有“行尾变化”:
git diff --ignore-space-at-eol
✅ 怎么修复(推荐做法:仓库级 + 编辑器级一起)
A. 给仓库加 .gitattributes(最稳,一劳永逸) 在仓库根目录添加/修改:
* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
解释:
- 默认所有文本统一用
LF - 但 Windows 脚本(bat/cmd)保留
CRLF
B. VS Code 里固定默认换行符 Settings 搜索:files.eol
设置为:
\n(LF)
或在 settings.json:
"files.eol": "\n"
2)第二嫌疑:Git 的 autocrlf 配置导致反复“全文件换行变化”
如果你本机 Git 配了 core.autocrlf=true,Git 在 checkout/commit 时会自动转换换行,配合 VS Code 又转换一次,很容易来回“打架”。
✅ 查看当前配置
git config --global --get core.autocrlf
git config --local --get core.autocrlf
✅ 推荐配置(多数跨平台工程)
-
跨平台(Windows+Linux)协作仓库:更推荐
git config --global core.autocrlf false
git config --global core.eol lf
-
然后靠
.gitattributes控制(最可靠) -
如果你只在 Windows 上开发、仓库也偏 Windows:可考虑
true
更多推荐
所有评论(0)