代码之家  ›  专栏  ›  技术社区  ›  paiego

为什么GIT中的文本行结尾不一致,尽管*text=auto

  •  0
  • paiego  · 技术社区  · 7 年前

    我正在克隆一个现有的GIT repo和.GIT属性 * text=auto !eol .

    根据我对文档的阅读,text=auto指定所有“text”文件都应该在repo中规范化。

    UTF-8,ISO-8859-1,)作为文本文件

    因此,我不知道如何在模糊的repo中检查行尾,但是在工作区中,我得到了文本(.java/.sql文件)的混合。有些是LF,有些是CRLF行尾。

    因为。是的 !eol

    1 回复  |  直到 7 年前
        1
  •  3
  •   Dietrich Epp    7 年前

    因为。是的 !eol

    是的,但只是因为 text=auto 并不意味着文件

    简而言之,你得到的是CRLF和LF的混合,因为回购是CRLF和LF的混合。全部 文本=自动 是不是这样的 新的 登记的东西会在回购中。

    例子

    我们先创建一个新的存储库,然后添加一个以CRLF行结尾的文本文件:

    $ git init
    Initialized empty Git repository in /.../test/.git/
    $ cat > file.txt
    line 1
    line 2
    $ unix2dos file.txt
    unix2dos: converting file file.txt to DOS format...
    $ git add file.txt
    $ git commit -m 'added file'
    [master (root-commit) f21d72f] added file
     1 file changed, 2 insertions(+)
     create mode 100644 file.txt
    

    工作副本中的文件都有CRLF。

    我们现在开始 text=auto eol . 这不会更改文件或将其标记为脏文件。还是CRLF。

    $ cat > .gitattributes
    * text=auto !eol
    $ git add .
    $ git commit -m 'add .gitattributes'
    [master 1844576] add .gitattributes
     1 file changed, 1 insertion(+)
     create mode 100644 .gitattributes
    $ file file.txt  
    file.txt: ASCII text, with CRLF line terminators
    $ git status
    On branch master
    nothing to commit, working tree clean
    

    我们可以删除文件并签出它,但是因为存储库中的文件有CRLF,所以签出时仍然得到CRLF。

    $ rm file.txt
    $ git checkout -- file.txt
    $ file file.txt
    file.txt: ASCII text, with CRLF, LF line terminators
    

    现在让我们用CRLF添加一个新文件。这将在存储库中得到规范化,但工作副本将不受影响。

    $ cat > file2.txt
    line 3
    line 4
    $ unix2dos file2.txt
    unix2dos: converting file file2.txt to DOS format...
    $ git add file2.txt
    warning: CRLF will be replaced by LF in file2.txt.
    The file will have its original line endings
    in your working directory
    $ git commit -m 'added file 2'
    [master cc2c5c3] added file 2
     1 file changed, 2 insertions(+)
     create mode 100644 file2.txt
    $ file file.txt file2.txt
    file.txt:  ASCII text, with CRLF line terminators
    file2.txt: ASCII text, with CRLF line terminators
    

    $ git status
    On branch master
    nothing to commit, working tree clean
    $ git checkout -- file.txt file2.txt
    $ file file.txt file2.txt
    file.txt:  ASCII text, with CRLF line terminators
    file2.txt: ASCII text, with CRLF line terminators
    

    我们可以在 然后再检查一遍:

    $ rm file2.txt
    $ git checkout -- file2.txt
    $ file file2.txt
    file2.txt: ASCII text
    

    设置eol=lf不会修复存储库

    eol 只有当存储库包含正确规范化的文件时,设置才真正起作用。改变它并不能解决我们的问题 file.txt .

    $ cat >.gitattributes
    *.txt text eol=lf
    $ git commit -m 'set eol=lf'
    [master c9e346b] set eol=lf
     1 file changed, 1 insertion(+), 1 deletion(-)
    $ rm file.txt
    $ git checkout -- file.txt
    $ file file.txt
    file.txt: ASCII text, with CRLF line terminators
    

    如何修复

    dos2unix