Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maboloshi/7d5e3d3753432da295e64728eb824f0d to your computer and use it in GitHub Desktop.
Save maboloshi/7d5e3d3753432da295e64728eb824f0d to your computer and use it in GitHub Desktop.

借助git快速批量转换CRLF到LF


换行符的差异

  • windows下每行结尾为回车+换行(CR+LF),即 \r\n
  • unix和macOS下每行结尾为换行LF,即 \n
  • classic macOS(最后一个版本为1999年发布的Mac OS 9,可忽略)下为回车,即 \r

设置jetbrain系IDE

settings > Editor > Code Style > Line Separator > unix and macOS (\n)

批量转换crlf文件为lf

autocrlf是git的一个配置

git config core.autocrlf val

autocrlf = true 表示要求git在提交时将crlf转换为lf,而在检出时将crlf转换为lf autocrlf = false表示提交和检出代码时均不进行转换 autocrlf = input 表示在提交时将crlf转换为lf,而检出时不转换

借助git的这个特性可以进行批量转换

  1. 新建空白文件夹,复制需要转换的文件到此文件夹
  2. 初始化此文件夹为git仓库并提交
  3. 删掉全部文件,然后还原,新文件现在全部是lf换行
  4. 用新文件覆盖原来的
cd temp
git init
git config core.autocrlf true
git add .
git commit -m "init"
rm -rf *
git reset --hard HEAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment