Git diff -w ignore whitespace only at start & end of lines

后端 未结 2 1770
失恋的感觉
失恋的感觉 2021-01-29 17:08

I love to use git diff -w to ignore whitespace differences. But, I just noticed that it ignores even whitespace differences in the middle of lines. How could I only

2条回答
  •  囚心锁ツ
    2021-01-29 17:55

    This is an old question, but is still regularly viewed/needed. I want to post to caution readers like me that whitespace as mentioned in the OP's question is not the same as Regex's definition, to include newlines, tabs, and space characters -- Git asks you to be explicit. See some options here: https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration

    As stated, git diff -b or git diff --ignore-space-change will ignore spaces at line ends. If you desire that setting to be your default behavior, the following line adds that intent to your .gitconfig file, so it will always ignore the space at line ends:

    git config --global core.whitespace trailing-space

    In my case, I found this question because I was interested in ignoring "carriage return whitespace differences", so I needed this:

    git diff --ignore-cr-at-eol or git config --global core.whitespace cr-at-eol from here.

    You can also make it the default only for that repo by omitting the --global parameter, and checking in the settings file for that repo. For the CR problem I faced, it goes away after check-in if warncrlf or autocrlf = true in the [core] section of the .gitconfig file.

提交回复
热议问题