解决Git自动换行问题

问题描述

main上拉取代码修改后,如果使用git bash执行git add .会弹出警告

warning: in the working copy of 'cmd/srv-transmission-line/apis/sys_log/create.go', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'cmd/srv-transmission-line/apis/sys_log/zz_generated.operator.go', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'cmd/srv-transmission-line/apis/sys_log/zz_generated.runtimedoc.go', LF will be replaced by CRLF the next time Git touches it

提示关于换行符(line endings)的变化。警告内容表明在某些文件中,工作目录中的换行符 LF(Line Feed)将被替换为 CRLF(Carriage Return + Line Feed),

在类Unix系统(如Linux和macOS)中,通常使用 LF 作为换行符,而在Windows系统中,通常使用 CRLF。当协同开发者使用不同系统时,这些换行符的变化也会被认为是修改了文件,导致可能只修改了某一个文件,而推上去后发现是所有文件都被标记为修改。这种发生是由于其他人是linux平台,我是windows,在git pull时会自动转换,导致不同操作系统的协同操作产生换行符混乱,因此需要进行处理。

因此需要在linux平台下执行git操作,避免换行符问题。如果使用虚拟机太麻烦,可以通过安装WSL可以解决问题

安装WSL

适用于 Linux 的 Windows 子系统 (WSL) 是 Windows 的一项功能,可用于在 Windows 计算机上运行 Linux 环境,而无需单独的虚拟机或双引导。 WSL 旨在为希望同时使用 Windows 和 Linux 的开发人员提供无缝高效的体验。

直接在PowerShell下执行

wsl --install

重启电脑,打开ubuntu图标,参照官网文档

Git配置

一般是自带git的,安装好之后执行以下命令查看版本,后面 Git 凭据管理器设置 有用

gala@GcGala:~$ git --version
git version 2.34.1

设置邮件和用户名。( win 设置过,不知道能不能和 win 共用,还是单独的,这里的设置和 win 一样的,ssh设置是可以共用的)

git config --global user.email "youremail@domain.com"
git config --global user.name "Your Name"

如果版本为 < v2.36.1,请输入此命令:

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager-core.exe"

配置ssh,wslwindows可以共用,先查看是否存在 SSH 密钥

ls -al ~/.ssh

如果有,还需要检查 SSH Agent 在当前 WSL 中是否运行 ,如果返回pid就证明正在运行

#启动 SSH Agent
eval $(ssh-agent)
#添加rsa密钥
ssh-add ~/.ssh/id_rsa

如果添加时报错.ssh/id_rsa: No such file or directory表明该密钥在~/.shh目录下不存在

#当前目录下没有密钥文件
ls -l ~/.ssh
total 4
-rw-r--r-- 1 gala gala 142 Jan 15 15:43 known_hosts

需要从C盘拷贝过去

cp /mnt/c/Users/YourUsername/.ssh/id_rsa ~/.ssh/
cp /mnt/c/Users/YourUsername/.ssh/id_rsa.pub ~/.ssh/

设置密钥权限,重新ssh-add

chmod 600 ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa

 

解决行结束问题

参考文档提到了三种方法,采用了其中一种

git config --global core.autocrlf false

配置完成后要重新pull,因为自动换行是在这个时候进行的

posted @ 2024-01-16 11:06  励志成为蔡徐坤  阅读(170)  评论(0编辑  收藏  举报