去掉行尾的^M

1. 处理掉行尾的^M

在windos下进行linux内核驱动编写,调试成功后需要集成到内核代码中去,所以会通过虚拟机共享文件夹拷贝到内核对应目录,这时候看源码文件还是没有异常的。

当对该文件进行回车换行操作后,还是没有异常,

但是通过git diff指令查看差异时,则会出现如下情况,很是不爽

解决办法:

  将源文件由windos环境 拷贝到 linux环境 后,使用"dos2unix file.c"工具命令将file.c的文件格式转换成unix格式,则彻底解决该问题。

 2. 忽略掉^M

下面简单配置指令可以让git diff的时候忽略换行符的差异:

git config --global core.whitespace cr-at-eol

更好的方法是每个项目都有一个.gitattributes文件,里面配好了换行符的设置,参考https://help.github.com/articles/dealing-with-line-endings

Here's an example .gitattributes file. You can use it as a template for your repositories:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

 

原文地址:https://www.cnblogs.com/emlslxl/p/5897372.html