Git warning: LF will be replaced by CRLF in XXX

本文解析了在Git中遇到的LF与CRLF换行符转换警告,阐述了其背后的原因,包括跨平台代码共享及Git默认设置的影响,并提供了三种配置解决方案,帮助开发者在Windows环境下更顺畅地进行跨平台协同开发。

1. 问题描述

在git add . 的时候出现如下Warning:

警告:待add的文件(App.vue, main.js...)中的LF将会使用CRLF替换,但工作区中的文件仍然使用原有的结束符。

( LF与CRLF都是结束符,文末有详细说明 )


2. 原因简介

出现上述警告的原因可能是:

①你使用了到别人的开源代码,该代码开发于非Windows环境

②与你协同开发的同事A使用非Windows系统提交代码

③Git在Windows环境下默认设置core.autocrlf为true

如下是core.autocrlf可选值及其作用:

简单翻译:

true当签出文本文件时Git自动将其LF换成CRLF,在提交时再自动换回LF;在Windows环境跨平台协同开发时推荐使用此值
input签出文件时不转换,提交文本文件时将CRLF转换成LF;在Unix环境跨平台协同开发时推荐使用此值
false签出/提交代码时都不会做任何转换操作;单一Windows环境下开发时推荐此值

3. 解决方法

git config core.autocrlf false

git config core.autocrlf input

git config core.autocrlf     // ==> true

//上述修改仅对当前Git仓库生效, 如需全局可添加 --global 参数

// Bonus time

不同系统换行符的不同表示:

系统换行符全称
Linux/UnixLFLineFeed(换行)
MacCRCarriageReturn(回车)
WindowsCRLFCarriageReturn+LineFeed(回车+换行)

 

warning: in the working copy of 'basic-spring-boot/.gitignore', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/pom.xml', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/main/java/com/tplink/nbu/demo/basicspringboot/BasicSpringBootApplication.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/main/java/com/tplink/nbu/demo/basicspringboot/controller/UserController.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/main/java/com/tplink/nbu/demo/basicspringboot/dto/UserLogoutDTO.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/main/java/com/tplink/nbu/demo/basicspringboot/dto/UserLogoutSuccessDTO.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/main/java/com/tplink/nbu/demo/basicspringboot/dto/UserRegisterDTO.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/main/java/com/tplink/nbu/demo/basicspringboot/dto/UserRegisterSuccessDTO.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/main/java/com/tplink/nbu/demo/basicspringboot/repository/UserRepository.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/main/resources/application.properties', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/test/java/com/tplink/nbu/demo/basicspringboot/BasicSpringBootApplicationTests.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/test/java/com/tplink/nbu/demo/basicspringboot/controller/UserControllerTest.java', LF will be replaced by CRLF the next time Git touches it warning: in the working copy of 'basic-spring-boot/src/test/java/com/tplink/nbu/demo/basicspringboot/service/UserServiceImplTest.java', LF will be replaced by CRLF the next time Git touches it git add .时出现这种警告
最新发布
08-23
14:26:56.791: [aaa] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:refs/heads/master --set-upstream --tags Enumerating objects: 62, done. Counting objects: 1% (1/62) Counting objects: 3% (2/62) Counting objects: 4% (3/62) Counting objects: 6% (4/62) Counting objects: 8% (5/62) Counting objects: 9% (6/62) Counting objects: 11% (7/62) Counting objects: 12% (8/62) Counting objects: 14% (9/62) Counting objects: 16% (10/62) Counting objects: 17% (11/62) Counting objects: 19% (12/62) Counting objects: 20% (13/62) Counting objects: 22% (14/62) Counting objects: 24% (15/62) Counting objects: 25% (16/62) Counting objects: 27% (17/62) Counting objects: 29% (18/62) Counting objects: 30% (19/62) Counting objects: 32% (20/62) Counting objects: 33% (21/62) Counting objects: 35% (22/62) Counting objects: 37% (23/62) Counting objects: 38% (24/62) Counting objects: 40% (25/62) Counting objects: 41% (26/62) Counting objects: 43% (27/62) Counting objects: 45% (28/62) Counting objects: 46% (29/62) Counting objects: 48% (30/62) Counting objects: 50% (31/62) Counting objects: 51% (32/62) Counting objects: 53% (33/62) Counting objects: 54% (34/62) Counting objects: 56% (35/62) Counting objects: 58% (36/62) Counting objects: 59% (37/62) Counting objects: 61% (38/62) Counting objects: 62% (39/62) Counting objects: 64% (40/62) Counting objects: 66% (41/62) Counting objects: 67% (42/62) Counting objects: 69% (43/62) Counting objects: 70% (44/62) Counting objects: 72% (45/62) Counting objects: 74% (46/62) Counting objects: 75% (47/62) Counting objects: 77% (48/62) Counting objects: 79% (49/62) Counting objects: 80% (50/62) Counting objects: 82% (51/62) Counting objects: 83% (52/62) Counting objects: 85% (53/62) Counting objects: 87% (54/62) Counting objects: 88% (55/62) Counting objects: 90% (56/62) Counting objects: 91% (57/62) Counting objects: 93% (58/62) Counting objects: 95% (59/62) Counting objects: 96% (60/62) Counting objects: 98% (61/62) Counting objects: 100% (62/62) Counting objects: 100% (62/62), done. Delta compression using up to 16 threads Compressing objects: 2% (1/48) Compressing objects: 4% (2/48) Compressing objects: 6% (3/48) Compressing objects: 8% (4/48) Compressing objects: 10% (5/48) Compressing objects: 12% (6/48) Compressing objects: 14% (7/48) Compressing objects: 16% (8/48) Compressing objects: 18% (9/48) Compressing objects: 20% (10/48) Compressing objects: 22% (11/48) Compressing objects: 25% (12/48) Compressing objects: 27% (13/48) Compressing objects: 29% (14/48) Compressing objects: 31% (15/48) Compressing objects: 33% (16/48) Compressing objects: 35% (17/48) Compressing objects: 37% (18/48) Compressing objects: 39% (19/48) Compressing objects: 41% (20/48) Compressing objects: 43% (21/48) Compressing objects: 45% (22/48) Compressing objects: 47% (23/48) Compressing objects: 50% (24/48) Compressing objects: 52% (25/48) Compressing objects: 54% (26/48) Compressing objects: 56% (27/48) Compressing objects: 58% (28/48) Compressing objects: 60% (29/48) Compressing objects: 62% (30/48) Compressing objects: 64% (31/48) Compressing objects: 66% (32/48) Compressing objects: 68% (33/48) Compressing objects: 70% (34/48) Compressing objects: 72% (35/48) Compressing objects: 75% (36/48) Compressing objects: 77% (37/48) Compressing objects: 79% (38/48) Compressing objects: 81% (39/48) Compressing objects: 83% (40/48) Compressing objects: 85% (41/48) Compressing objects: 87% (42/48) Compressing objects: 89% (43/48) Compressing objects: 91% (44/48) Compressing objects: 93% (45/48) Compressing objects: 95% (46/48) Compressing objects: 97% (47/48) Compressing objects: 100% (48/48) Compressing objects: 100% (48/48), done. Writing objects: 1% (1/62) Writing objects: 3% (2/62) Writing objects: 4% (3/62) Writing objects: 6% (4/62) Writing objects: 8% (5/62) Writing objects: 9% (6/62) Writing objects: 11% (7/62) Writing objects: 12% (8/62) Writing objects: 14% (9/62) Writing objects: 16% (10/62) Writing objects: 17% (11/62) Writing objects: 19% (12/62) Writing objects: 20% (13/62) Writing objects: 22% (14/62) Writing objects: 24% (15/62) Writing objects: 25% (16/62) Writing objects: 27% (17/62) Writing objects: 29% (18/62) Writing objects: 30% (19/62) Writing objects: 32% (20/62) Writing objects: 33% (21/62) Writing objects: 35% (22/62) Writing objects: 37% (23/62) Writing objects: 38% (24/62) Writing objects: 40% (25/62) Writing objects: 41% (26/62) Writing objects: 43% (27/62) Writing objects: 45% (28/62) Writing objects: 46% (29/62) Writing objects: 48% (30/62) Writing objects: 50% (31/62) Writing objects: 51% (32/62) Writing objects: 53% (33/62) Writing objects: 54% (34/62) Writing objects: 56% (35/62) Writing objects: 58% (36/62) Writing objects: 59% (37/62) Writing objects: 61% (38/62) Writing objects: 62% (39/62) Writing objects: 64% (40/62) Writing objects: 66% (41/62) Writing objects: 67% (42/62) Writing objects: 69% (43/62) Writing objects: 70% (44/62) Writing objects: 72% (45/62) Writing objects: 74% (46/62) Writing objects: 75% (47/62) Writing objects: 77% (48/62) Writing objects: 79% (49/62) Writing objects: 80% (50/62) Writing objects: 82% (51/62) Writing objects: 83% (52/62) Writing objects: 85% (53/62) Writing objects: 87% (54/62) Writing objects: 88% (55/62) Writing objects: 90% (56/62) Writing objects: 91% (57/62) Writing objects: 93% (58/62) Writing objects: 95% (59/62) Writing objects: 96% (60/62) Writing objects: 98% (61/62) Writing objects: 100% (62/62) Writing objects: 100% (62/62), 117.54 KiB | 4.35 MiB/s, done. Total 62 (delta 3), reused 0 (delta 0), pack-reused 0 (from 0) remote: Powered by GITEE.COM [1.1.5] remote: Set trace flag bd4e25b8 To https://gitee.com/shangxie26/aaa.git * refs/heads/master:refs/heads/master [new branch] branch 'master' set up to track 'origin/master'. Done 14:27:26.440: [aaa] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:master --tags To https://gitee.com/shangxie26/aaa.git = refs/heads/master:refs/heads/master [up to date] Done 14:50:51.641: [aaa] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- app/src/main/ets/pages/Index.ets warning: in the working copy of 'app/src/main/ets/pages/Index.ets', LF will be replaced by CRLF the next time Git touches it 14:50:51.744: [aaa] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\jie72\AppData\Local\Temp\git-commit-msg-.txt -- [master 33cf0c6] 新增仓库 1 file changed, 1 insertion(+) 14:50:54.560: [aaa] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:master --tags Warning: Permanently added 'github.com' (ED25519) to the list of known hosts. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 14:53:06.952: [aaa] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false fetch origin --recurse-submodules=no --progress --prune git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 14:55:00.770: [aaa] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false fetch origin --recurse-submodules=no --progress --prune git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 15:08:44.327: [aaa] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/master:master --tags Warning: Permanently added 'github.com' (ED25519) to the list of known hosts. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
07-18
### 问题分析 在 Git 中,当你在 Windows 系统上使用 `git add .` 命令时,出现警告信息 `warning: LF will be replaced by CRLF in Makefile`,这是因为 Git 的自动换行符转换机制(`core.autocrlf`)被启用。 LF(Line Feed)是 Unix/Linux 系统中的换行符,而 CRLF(Carriage Return + Line Feed)是 Windows 系统中的换行符。Git 为了统一跨平台协作,提供了自动转换换行符的功能,但在某些情况下会导致文件被误认为是被修改过,从而触发此警告。 ### 解决方案 #### 方法一:禁用自动换行符转换 如果希望保留原始换行符格式,可以禁用 Git 的自动换行符转换功能: ```bash git config --global core.autocrlf false ``` 这样,Git 将不会对文件的换行符进行自动转换,从而避免警告信息的出现。 #### 方法二:设置为仅提交时转换 如果你希望在提交时将 CRLF 转换为 LF,但在检出时保持 LF,可以使用以下命令: ```bash git config --global core.autocrlf input ``` 此设置适用于 Unix/Linux 和 macOS 用户,确保提交时换行符为 LF,而在检出时不进行转换。 #### 方法三:手动处理文件换行符 如果你只想针对特定文件(如 `Makefile`)处理换行符问题,可以使用 `.gitattributes` 文件来定义特定文件的换行符处理方式。例如: 1. 在项目根目录下创建或编辑 `.gitattributes` 文件。 2. 添加以下内容: ```plaintext Makefile text eol=lf ``` 此配置会强制 Git 在处理 `Makefile` 文件时使用 LF 作为换行符,避免自动转换。 #### 方法四:清理并重新添加文件 如果已经存在换行符不一致的问题,可以尝试以下步骤清理并重新提交文件: ```bash git rm --cached -r . git reset git add . git commit -m "Fix line endings" ``` 这将重新缓存所有文件,并按照当前 `core.autocrlf` 设置处理换行符。 ### 注意事项 - 修改 `core.autocrlf` 配置后,建议重新初始化 Git 缓存,以确保更改生效。 - 如果项目是跨平台协作的,建议团队成员统一换行符处理策略,以避免不必要的冲突和警告。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值