git and CR vs LF (but NOT CRLF)

后端 未结 2 1392
孤独总比滥情好
孤独总比滥情好 2021-02-04 16:28

This may sound like a redundant question (and may very well be a redundnant question) but I can\'t find the answer. Here\'s the situation:

My application is creating te

2条回答
  •  抹茶落季
    2021-02-04 17:11

    Git doesn’t appear to support CR line endings, so I would write a filter to convert the newlines. The files in the work tree will have CR line endings, and they will be transparently converted to LF when they are indexed. The filter has two parts: “clean” checks the files in, and “smudge” checks the files out.

    Use this in .git/config:

    [filter "cr"]
        clean = tr '\\r' '\\n'
        smudge = tr '\\n' '\\r'
    

    And .git/info/attributes (or .gitattributes if it should be versioned)

    * filter=cr
    

    Note that this automatically makes git-diff happy, since it will use the “clean” version.

    Just remember to set the pattern to just the files you need, or binary files will be corrupted and all text files will check out with CR line endings.

    Also note that if the filter is not configured, it will silently fail, so add the config line when setting up a new copy of the repository.

提交回复
热议问题