可通过配置实现行尾符标准化处理。方法如下:
- 方法一,全局配置:
通过core.autocrlf
参数设置自动转换规则:
- windows用户:
git config --global core.autocrlf true
- linux/mac用户:
git config --global core.autocrlf input
- 方法二,项目级配置(推荐)
在仓库目录创建.gitattributes
文件,指定具体规则:
* text=auto # 自动识别文本文件并转换行尾符
*.sh text eol=lf # 强制.sh文件使用LF
*.bat text eol=crlf # 强制.bat文件使用CRLF
*.png binary # 标记二进制文件不处理
这种方式会覆盖全局配置,确保所有协作者的行尾符一致。
3. 手动修复已存在的行尾问题
使用命令 git rm --cached -r . 和 git reset --hard 重新检出文件以应用新规则。