【问题标题】:Line endings cohabiting Terminal and Eclipse Git行尾同居终端和 Eclipse Git
【发布时间】:2018-07-09 06:38:04
【问题描述】:

我们正在处理一个项目并使用 Git 管理我们的代码版本。我们每个功能有一个分支,我们维护的每个版本都有一个分支...问题是,我们使用命令行将项目连接到 git(Eclipse 没有提供足够的功能来正确执行此操作)但通常我们使用 Eclipse 提交, 推... 另外有时Eclipse不想让我们push,我们只能在命令行下push。

这种情况导致远程上的一些文件以 crlf 行结尾,而另一些文件以 lf 行结尾。所有这些都是由 Eclipse 和终端在 libne 结尾处表现不同引起的,我意识到为时已晚。我现在正在寻找一种简单、正确的方法来使所有文件在 true 时自动 crlf,以及在所有 git 客户端上保证终端和 Eclipse 上相同行为的过程(基于终端上的行为)。

(抱歉英语不好,提前感谢)!

--编辑--

我应该准确地说我正在寻找一种解决方案,该解决方案也适用于转换现有文件,将远程文件转换为远程上的 lf 和存储库上的 crlf

【问题讨论】:

    标签: eclipse git terminal line-endings


    【解决方案1】:

    您可以尝试使用此条目在存储库中添加和提交.gitattributes 文件。

    * text eol=crlf
    

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

    Git 总是会在结账时将行尾转换为 CRLF。你应该 将此用于必须保留 CRLF 结尾的文件,即使在 OSX 或 Linux 上也是如此。

    插图:

    考虑现有 repo 中有 2 个文件(比如 test-repo),每个文件各一行。

    file_a.txt
    file_b.txt
    

    验证文件结尾为\n

    test-repo (master)$ od -c file_a.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   a  \n
    0000017
    
    test-repo (master)$ od -c file_b.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   b  \n
    0000017
    

    在存储库中添加一个.gitattributes 文件。

    test-repo (master)$ vim .gitattributes
    

    添加条目。

    * text eol=crlf
    

    提交并推送.gitattributes

    test-repo (master)$ git add -A
    warning: LF will be replaced by CRLF in .gitattributes.
    The file will have its original line endings in your working directory.
    
    test-repo (master)$ git commit -m "Adding .gitattributes file"
    
    test-repo (master)$ git push
    

    添加一个新文件,并在其中键入一行文本。

    test-repo (master)$ vim file_c.txt
    

    验证以\n结尾的文件。

    test-repo (master)$ od -c file_c.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   c  \n
    0000017
    

    提交并推送添加的文件。

    test-repo (master)$ git add file_c.txt
    warning: LF will be replaced by CRLF in file_c.txt.
    The file will have its original line endings in your working directory.
    
    test-repo (master)$ git commit -m "Added file_c.txt"
    
    test-repo (master)$ git push
    

    将存储库克隆到磁盘上的另一个目录(例如名称为temp_dir)。请注意,结尾现在是 \r \n 代替 \n

    temp_dir/test-repo (master) $ od -c file_a.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   a  \r  \n
    0000020
    
    temp_dir/test-repo (master) $ od -c file_b.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   b  \r  \n
    0000020
    
    temp_dir/test-repo (master) $ od -c file_c.txt
    0000000    T   h   i   s       i   s       f   i   l   e   _   c  \r  \n
    0000020
    

    【讨论】:

    • 感谢您的建议,您的解决方案适用于新文件,我实际上正在寻找远程现有文件的解决方案,将远程上的所有文件转换为 lf,存储库上的所有文件在 crlf 上并为将来的文件保留终端行为(我应该更清楚地说明这一点)
    • @AmaniteLaurine:它也适用于现有文件。请记住,任何已提交的内容都无法更改,因此任何 commit 中的文件都不会更改。但是,重要的是要记住 Git 在 (1) 已提交文件、(2) 索引/暂存区域中的建议提交文件和 (3) 工作树中的文件之间的区别。 eol=crlf 设置影响工作树中的文件,包括从提交中提取的现有文件,当它们从提交移动到索引/暂存区域然后再打开时到工作树。
    • Git 有几个控制选项来处理已经以错误格式提交的文件,包括merge.renormalize。现有提交中存储的版本不会更改,但 Git 通过在执行合并操作之前提取并重新提交来进行新的“虚拟提交”。这会造成时间损失,但它绕过了现有提交的只读性质。
    • 感谢您的提示。我发现 Eclipse 在这里不支持 gitattributes :bugs.eclipse.org/bugs/show_bug.cgi?id=342372 错误的状态仍然是“已分配”,我想它还没有修复,但是还有其他方法可以让 Eclipse 支持 gitattributes 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多