Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scmiot/a98fad2449dd6dd495bbe6fc55e7dbbc to your computer and use it in GitHub Desktop.
Save scmiot/a98fad2449dd6dd495bbe6fc55e7dbbc to your computer and use it in GitHub Desktop.

Revisions

  1. @atez atez revised this gist May 22, 2019. No changes.
  2. @atez atez created this gist May 22, 2019.
    36 changes: 36 additions & 0 deletions 借助git快速批量转换CRLF到LF.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    # 借助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的一个配置
    ```bash
    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. 用新文件覆盖原来的

    ```bash
    cd temp
    git init
    git config core.autocrlf true
    git add .
    git commit -m "init"
    rm -rf *
    git reset --hard HEAD
    ```