What's the best CRLF (carriage return, line feed) handling strategy with Git?

后端 未结 9 1969
青春惊慌失措
青春惊慌失措 2020-11-22 07:30

I tried committing files with CRLF-ending lines, but it failed.

I spent a whole work day on my Windows computer trying different strategies and was almost drawn to s

9条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 08:33

    Two alternative strategies to get consistent about line-endings in mixed environments (Microsoft + Linux + Mac):

    A. Global per All Repositories Setup

    1) Convert all to one format

    find . -type f -not -path "./.git/*" -exec dos2unix {} \;
    git commit -a -m 'dos2unix conversion'
    

    2) Set core.autocrlf to input on Linux/UNIX or true on MS Windows (repository or global)

    git config --global core.autocrlf input
    

    3) [ Optional ] set core.safecrlf to true (to stop) or warn (to sing:) to add extra guard comparing if the reversed newline transformation would result in the same file

    git config --global core.safecrlf true
    


    B. Or per Repository Setup

    1) Convert all to one format

    find . -type f -not -path "./.git/*" -exec dos2unix {} \;
    git commit -a -m 'dos2unix conversion'
    

    2) add .gitattributes file to your repository

    echo "* text=auto" > .gitattributes
    git add .gitattributes
    git commit -m 'adding .gitattributes for unified line-ending'
    

    Don't worry about your binary files - Git should be smart enough about them.


    More about safecrlf/autocrlf variables

提交回复
热议问题