1 场景
我需要用到必需在Windows系统上创建的文本文件。因为这些文本只能由Windows系统上的Microsoft Acesss软件生成。
然后,我用这些新生成文本文件覆盖旧有的文本文件。我用git diff查看覆盖后的差异。
git diff
结果如下:
+BBB*^M
+AAA*^M
+YYY*^M
+XXX*^M
可以看到,增加的每一行文本的末尾都有额外的^M
字符。
但是用vim打开相应的文本文件,使用搜索命令
/\^M
并没有找到^M
字样
E486: Pattern not found: ^M
那么,为什么git diff查看到差异与用vim看到字符不一致呢?
首先,你要了解 line break types, 我引用其中一个回答:
This is a good summary I found:
The Carriage Return (CR) character (0x0D, \r) moves the cursor to the beginning of the line without advancing to the next line. This character is used as a new line character in Commodore and Early Macintosh operating systems (OS-9 and earlier).
The Line Feed (LF) character (0x0A, \n) moves the cursor down to the next line without returning to the beginning of the line. This character is used as a new line character in UNIX based systems (Linux, Mac OSX, etc)
The End of Line (EOL) sequence (0x0D 0x0A, \r\n) is actually two ASCII characters, a combination of the CR and LF characters. It moves the cursor both down to the next line and to the beginning of that line. This character is used as a new line character in most other non-Unix operating systems including Microsoft Windows, Symbian OS and others.
简单概括下:windows的文本默认以\r
作为一行的结束,类UNIX系统,包括macOS,的文本默认以\r\n
作为一行的结束。
行末多出的^M
字符正是出于这个原因:
In ASCII and Unicode, the carriage return is defined as 13 (or hexadecimal 0D); it may also be seen as control+M or ^M.
2 Solution
所以解决方案就呼之欲出了。Git需要知道^M
也是换行符的一部分。
git config --global core.whitespace cr-at-eol
This basically tells Git that an end-of-line CR is not an error.
As a result, those annoying ^M characters no longer appear at the end of lines in git diff, git show, etc.
详细解释下为什么这个gitconfig可行,详见此处
Git comes preset to detect and fix some whitespace issues. … and cr-at-eol, which tells Git that carriage returns at the end of lines are OK.