行结尾与Eclipse Git共存

行结尾与Eclipse Git共存,eclipse,git,terminal,line-endings,Eclipse,Git,Terminal,Line Endings,我们正在开发一个项目,并使用Git管理我们的代码版本。我们每个功能有一个分支,我们维护的每个版本有一个分支。。。问题是,我们使用命令行将项目连接到git(Eclipse没有提供足够的功能来正确实现),但通常我们使用Eclipse来提交、推送。。。 此外,有时Eclipse不想让我们推,我们被迫在命令行中推 这种情况导致远程上的一些文件位于crlf行结尾,而另一些文件位于lf行结尾。所有这些都是由日食和终端对libne结尾的不同行为造成的,我意识到这一切太晚了。我现在正在寻找一种简单、正确的方法,

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

这种情况导致远程上的一些文件位于crlf行结尾,而另一些文件位于lf行结尾。所有这些都是由日食和终端对libne结尾的不同行为造成的,我意识到这一切太晚了。我现在正在寻找一种简单、正确的方法,使所有文件自动crlf为true,并寻找一个过程,以保证在所有git客户机上的Terminal和Eclipse上的行为相同(基于Terminal的行为)

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

--编辑--


我本应该澄清一下,我正在搜索一种解决方案,该解决方案也适用于转换现有文件,将远程文件转换为远程上的lf和存储库上的crlf

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

* text eol=crlf
(参考: )

Git总是在签出时将行尾转换为CRLF。你应该 对于必须保留CRLF结尾的文件,即使在OSX或Linux上,也可以使用此选项

插图:

假设现有repo中有两个文件(比如
testrepo
),每个文件中有一行

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
test-repo (master)$ od -c file_c.txt
0000000    T   h   i   s       i   s       f   i   l   e   _   c  \n
0000017
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
在存储库中添加
.gittributes
文件

test-repo (master)$ vim .gitattributes
添加条目

* text eol=crlf
提交并推送
.gittributes

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_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
test-repo (master)$ od -c file_c.txt
0000000    T   h   i   s       i   s       f   i   l   e   _   c  \n
0000017
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
提交并推送添加的文件

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\u dir
)。请注意,现在结尾是
\r\n
而不是
\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
test-repo (master)$ od -c file_c.txt
0000000    T   h   i   s       i   s       f   i   l   e   _   c  \n
0000017
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:它也适用于现有文件。请记住,任何提交的文件都不能更改,因此任何提交更改中都没有文件。但是,重要的是要记住Git在(1)提交的文件,(2)索引/暂存区域中建议的提交文件和(3)工作树中的文件之间的区别。
eol=crlf
设置会影响工作树中的文件,其中包括从提交中提取的现有文件,当这些文件从提交区域移到索引/暂存区域,然后移到工作树时。Git有几个控制选项用于处理已以错误格式提交的文件,包括
merge.renormalize
。现有提交中存储的版本不会更改,但Git在执行合并操作之前通过提取和重新提交来进行新的“虚拟提交”。这是一个时间惩罚,但它绕过了现有提交的只读性质。谢谢提示。我发现eclipse在这里不支持gitattributes:bug的状态仍然是“分配的”,我想它还没有修复,但是有没有其他方法使eclipse支持gitattributes呢?