在Windows上把一个修改过的文件 copy 或者 commit 到Ubuntu,在Ubuntu上使用git status查看,发现没有修改的文件也提示修改未上传。

于是,用git diff查看文件差异,发现每一行结尾都有^M。上网查资料,了解了原因:

这是由于换行符在不同的操作系统上定义的区别造成的。

Windows用CR LF来定义换行,Linux用LF。CR全称是Carriage Return ,或者表示为\r, 意思是回车。 LF全称是Line Feed,它才是真正意义上的换行表示符。为什么Windows添加一个CR和LF组合表示,我并不清楚。不过如果用git diff的时候看到^M字符,就说明两个文件在换行符上有所差别。

比如从我的Windows开发的同时那边拿来一个目录,就会发现几乎所有的文件都被修改过了。其实并不是这样,都是由于文件多了CR后造成的。

GitHub的帮助网站上给出了一种**解决方案**:

参考:https://help.github.com/en/articles/dealing-with-line-endings

  • 在Windows的文件夹上新建一个.gitattributes文件

 内容如下:

# 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

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐