git - 为什么 git 在标记为二进制的文件上警告 CRLF?

标签 git

我有一个被标记为二进制的文件:

$ cat .gitattributes
dist/* binary
$ git check-attr -a ./dist/app.js
./dist/app.js: binary: set
./dist/app.js: diff: unset
./dist/app.js: merge: unset
./dist/app.js: text: auto

git diff 正确地将文件视为二进制文件:

$ git diff
diff --git a/dist/app.js b/dist/app.js
index 9c05798..fbcedd4 100644
Binary files a/dist/app.js and b/dist/app.js differ

但是当我运行 git status 时,我收到关于 CRLF 转换的警告:

$ git status
warning: CRLF will be replaced by LF in dist/app.js.
The file will have its original line endings in your working directory.
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   dist/app.js

这是怎么回事?为什么 git 警告我有关此文件中的 CRLF?

最佳答案

Git 2.10(2016 年第 3 季度)应该更加小心二进制中的 crlf。

参见 commit 6523728 (2016 年 6 月 28 日)作者:Torsten Bögershausen (tboegi) .
(由 Junio C Hamano -- gitster -- merge 于 commit 21bed62 ,2016 年 7 月 25 日)

convert: unify the "auto" handling of CRLF

Before this change,

$ echo "* text=auto" >.gitattributes
$ echo "* eol=crlf" >>.gitattributes

would have the same effect as

$ echo "* text" >.gitattributes
$ git config core.eol crlf

Since the 'eol' attribute had higher priority than 'text=auto', this may corrupt binary files and is not what most users expect to happen.

Make the 'eol' attribute to obey 'text=auto' and now

$ echo "* text=auto" >.gitattributes
$ echo "* eol=crlf" >>.gitattributes

behaves the same as

$ echo "* text=auto" >.gitattributes
$ git config core.eol crlf

Git 2.13(2017 年第 2 季度)确保规范化使用正确的命令
参见 commit 8599974 (2017 年 4 月 12 日)Torsten Bögershausen (tboegi) .
(由 Junio C Hamano -- gitster -- merge 于 commit 848d9a9 ,2017 年 4 月 24 日)

收件人trigger a re-normalization :

From a clean working directory:

$ echo "* text=auto" >.gitattributes
$ rm .git/index     # Remove the index to re-scan the working directory
$ git add .
$ git status        # Show files that will be normalized
$ git commit -m "Introduce end-of-line normalization"

随着 Git 2.36(2022 年第 2 季度),eol 属性文档已完成。

参见 commit 8c591db , commit ab96151 (2022 年 1 月 11 日)作者:brian m. carlson (bk2204) .
(由 Junio C Hamano -- gitster -- merge 于 commit 8db2f66 ,2022 年 2 月 11 日)

docs: correct documentation about eol attribute

Signed-off-by: brian m. carlson

The documentation for the eol attribute states that it is "effectively setting the text attribute".

However, this implies that it forces the text attribute to always be set, which has not been the case since 6523728 (convert: unify the , 2016-06-28, Git v2.10.0-rc0 -- merge listed in batch #7) ("convert: unify the "auto" handling of CRLF", 2016-06-28).

Let's avoid confusing users (and the present author when trying to describe Git's behavior to others) by clearly documenting in which cases the "eol" attribute has effect.

Specifically, the attribute always has an effect unless the file is explicitly set as -text, or the file is set as text=auto and the file is detected as binary.

gitattributes 现在包含在其 man page 中:

This attribute has effect only if the text attribute is set or unspecified, or if it is set to auto and the file is detected as text.

Note that setting this attribute on paths which are in the index with CRLF line endings may make the paths to be considered dirty.

Adding the path to the index again will normalize the line endings in the index.


为了进一步说明,Git 2.36(2022 年第 2 季度)更新了文档:

参见 commit 6a5678f (2022 年 2 月 14 日)brian m. carlson (bk2204) .
(由 Junio C Hamano -- gitster -- merge 于 commit 66633f2,2022 年 2 月 23 日)

doc: clarify interaction between 'eol' and text=auto

Signed-off-by: brian m. carlson

The eol takes effect on text files only when the index has the contents in LF line endings.
Paths with contents in CRLF line endings in the index may become dirty unless text=auto.

gitattributes 现在包含在其 man page 中:

This attribute sets a specific line-ending style to be used in the working directory.

This attribute has effect only if the text attribute is set or unspecified, or if it is set to auto, the file is detected as text, and it is stored with LF endings in the index.

Note that setting this attribute on paths which are in the index with CRLF line endings may make the paths to be considered dirty unless text=auto is set.
Adding the path to the index again will normalize the line endings in the index.

关于git - 为什么 git 在标记为二进制的文件上警告 CRLF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33643309/

相关文章:

git - 在项目之间共享 Github pull 请求模板的最佳方式?

git - 同步 TFS 和 GIT

ruby - 导入数千条记录后无法推送到 Heroku

git - 使用 git rewrite 重新格式化整个代码库

git - 如何 stash 当前文件夹中的更改?

Git 使用了错误的身份(.ssh/config 文件未读取?)

git - tortoisegit推送成功,但是没有新文件?

git - 什么是 git 守护进程?

git - 标记远程 git 存储库而不克隆它

git - 在亚马逊 AWS 上部署网站