首页 > 解决方案 > 行尾同居终端和 Eclipse Git

问题描述

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

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

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

- 编辑 -

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

标签: eclipsegitterminalline-endings

解决方案


.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

将存储库克隆到磁盘上的另一个目录(例如使用 name 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

推荐阅读