解决警告“..LF will be replaced by CRLF the next time Git touches it“

在windows下执行git add . 时出现很多类似如下的警告

复制代码
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_pka.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_pwr.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_rcc.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_rng.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_rtc.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_sdmmc.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_spi.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_swpmi.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_tim.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_usart.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_usb.c', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_utils.c', LF will be replaced by CRLF the next time Git touches it

警告含义是当前工作区里的文件都是"LF"结尾, 但现在Git配置的是core.autocrlf = true

所以下次GIt检出或者重写这些文件时, 会自动把LF转换成CRLF

然后git出现大量的提醒, 就是将来会执行这些转换.

为了不影响代码, 进行如下配置

复制代码
git config --global core.autocrlf false
git config --global core.safecrlf warn

然后统一项目使用 LF 行尾

在项目根目录执行

复制代码
git rm --cached -r . # 清空index
git reset --hard  # 重新checkout所有文件

然后重新执行

复制代码
git status
git add .

warning会全部消失

另外一种方式就是在项目根目录建立.gitattributes文件, 在其中添加

复制代码
* text=auto eol=lf
相关推荐
春日见6 小时前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
stevenzqzq1 天前
git 常用操作
大数据·git
Curvatureflight1 天前
Git工作流最佳实践:从混乱到优雅
git
wu~9701 天前
GitHub永不遗忘,使用git push -f来覆盖的提交依旧保留
git·github
Vermouth_001 天前
git clone的时候提示access denied
git
qq_437657271 天前
清楚本地的git并重新登录
git
jiang_changsheng1 天前
工作流agent汇总分析 2
java·人工智能·git·python·机器学习·github·语音识别
顶点多余1 天前
版本控制器-git
linux·git
夔曦1 天前
Git工程日常下拉/上传完整流程(自用)
git
岱宗夫up1 天前
GitHub Desktop如何设置中文?这不是个简单问题
git·github