How to make git understand Mac (CR) line endings

前端 未结 2 478
谎友^
谎友^ 2021-01-19 15:12

For some reasons one of my files contains old style Mac line endings (after editing on OSX). These are \"CR\" (carriage return) characters and show up as ^M in git dif

2条回答
  •  清歌不尽
    2021-01-19 16:06

    Since the accepted answer, a new way to do this has been introduced.

    You can teach git diff and git log to run the file through a special command before creating the diff. This is a one-way process, which is just used for generating diffs, and doesn't affect how the files are stored on disk or in your repository.

    Create a new diff driver called "cr", which runs the file through tr before calculating the diff. In your .git/config:

    [diff "cr"]
        textconv = tr '\\r' '\\n' <
    

    Alternatively:

    git config diff.cr.textconv "tr '\r' '\n' <"
    

    Then tell git to use it using your .gitattributes (e.g. for all .csv files):

    *.csv diff=cr
    

    Note that this only affects diffs. It won't help you with merging!

提交回复
热议问题