git - windows + linux 双引导中的 CRLF 问题

git - CRLF issue in windows + linux dual boot

我将用解决我问题的方法来回答我的问题。

投反对票者注意事项:我知道根本原因已在其他各种线程中讨论过(这就是我解决问题的方式)。这个post更多的是关于双引导系统如何导致你遇到这个问题。所以 不,这个 question/answer 不是重复的 ,而是一般 class 问题的一个特定实例,在这个问题上向 SO 的存储库添加了更多案例。


在家:我在Linux中编码。 LF 用作行结尾
在办公室:我在windows中编码。 CRLF 用作行尾。
默认情况下,git 的 autocrlf 功能 () 让事情变得愉快。

但是,如果您 运行 具有 Linux 和 Windows 的双引导系统,您可以通过以下方式搞砸自己:

  1. git pull 您在 windows 环境中的 linux 环境中处理的一些文件,位于可以从双引导 linux 环境访问的位置.这会修改文件以包含 CRLF 个结尾。
  2. 然后当你在linux中打开文件时,默认只有LFgit diff会说整个文件被修改,因为每个 LF 在每一行都更改为 CRLF。 (我被 Atom 警告过,它内置了这个差异计数)

FIX

  1. Delete/move问题换个位置files/folders
  2. git checkout <hash> <your/files/location>

其中 <hash> 是最后一次好的提交,your/files/location 是您要从 CRLF 治愈 的文件的位置] 问题。这基本上会从您的本地 .git 存储库恢复旧版本。

对我有用。


如果您知道我遗漏或解释不正确的内容,请告诉我

you are talking about text=auto part? I wasn't sure If I need to include that in a new .gitattributes file or not, because when I did, ATOM still showed the files as modified.

是的,确实如此。
要强制 Git 应用 .gitattributes 指令,请参阅“Dealing with line endings”。

我会首先确保 core.autocrlf 设置为 false。

git config --global core.autocrlf false

然后:

git add . -u
git commit -m "Saving files before refreshing line endings"

rm .git/index

git reset

git status

git add -u
git add .gitattributes

git commit -m "Normalize all the line endings"

你也可以使用,强制索引重新归一化:

git rm --cached -r .
git reset --hard

参见“Force LF eol in git repo and working copy

* text=auto eol=lf