使用git diff时,忽略额外的^M字符

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.

diff中出现^M字符的问题,其实是由于不同操作系统的换行符导致的。在Windows系统中,换行符是由\r\n组成的,而在Unix和Linux系统中,换行符只有\n。当你在一个操作系统上编辑文件,然后在另一个操作系统上查看,就会出现换行符的差异。 为了解决这个问题,你可以使用git的配置来处理换行符的差异。其中一种解决方案是通过设置core.whitespace选项为cr-at-eol,可以忽略换行符的差异。你可以在命令行中运行以下命令来设置这个选项: git config --global core.whitespace cr-at-eol 另一种解决方案是通过设置core.autocrlf选项为input来在提交换行符转换为LF。你可以在命令行中运行以下命令来设置这个选项: git config --global core.autocrlf input 这些配置可以帮助你在git diff中正确显示修改点,避免出现^M字符。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [解决git diff的^M问题](https://blog.csdn.net/w727655308/article/details/125616499)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [git diff 查看修改出现 ^M 换行符不匹配](https://blog.csdn.net/xy862578733/article/details/124165748)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值