解决警告“..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
相关推荐
李少兄2 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
先跑起来再说8 小时前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
承渊政道11 小时前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
Doro再努力11 小时前
【Linux操作系统12】Git版本控制与GDB调试:从入门到实践
linux·运维·服务器·git·vim
摇滚侠13 小时前
MAC IDEA GIT 提交区显示了几个不存在的目录
git·idea
城东14 小时前
Git使用[远程仓库远端的head比本地和提交的head旧,其他人拉不到最新代码]
git·head·远程仓库远端·比本地和提交的head旧·其他人拉不到最新代码
何中应1 天前
使用SSH地址拉取远程仓库代码报下面的错误
git
何中应1 天前
Git本地仓库命令补充
git
sun0077001 天前
执行repo sync -c -d -j4以后,提交未git push的代码看不到了。要怎么恢复?
git
胖虎11 天前
Git 一个本地仓库同时推送到两个远程仓库(详细教程)
git·多远程仓库·双远程仓库·git双远程·git备份