`git status`和EOL检测

时间:2014-11-25 14:24:49

标签: git newline

当我git status时,我看到了这一点:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   project/schema-readme.md
        modified:   project/vp-automate.php

奇怪的是,这两个文件实际修改了 - 它们是二进制相同的,逐字节。

现在我想想我知道为什么他们被Git标记为修改:这是因为他们使用CRLF行结尾而我们的项目最近添加了一个.gitattributes文件包含这个内容:

* text=auto eol=lf

这应该意味着当提交时,Git实际上会更新它们以获得LF行结尾。这就是git status将这些文件标记为“已修改”的原因,即使它们当前未被修改过吗?

编辑:这很奇怪,我的项目中有更多的文件使用CRLF行结尾并且没有被检测为修改过..我不明白schema-readme.mdvp-automate.php有什么特别之处Git将它们检测为已修改。

2 个答案:

答案 0 :(得分:1)

除了SHA1校验和之外,Git还会根据某些文件属性检测更改的文件,这可能导致这两个文件被标记为已更改。有关Git如何检测文件更改的更多信息,请参见answer to this question

答案 1 :(得分:0)

请尝试配置core.autoccrlf属性。它主要用于Windows机器上,以确保本地文件使用CRLF进行行结束(因此它们可以在记事本中工作等),而文件&#34;内部&#34;存储库保存在&#34;适当的&#34; LF结局。

来自docs(git config --help):

   core.autocrlf
       Setting this variable to "true" is almost the same as setting the text attribute to "auto" on all files except that text files are not guaranteed to be normalized: files that contain CRLF in the repository will not be touched. Use this setting if you want to have CRLF line endings in
       your working directory even though the repository does not have normalized line endings. This variable can be set to input, in which case no output conversion is performed.

另见https://help.github.com/articles/dealing-with-line-endings#platform-all - 按照建议,在linux上你应该将它配置为&#34;输入&#34;。