行尾与 Terminal 和 Eclipse 同居 Git

Line endings cohabiting Terminal and Eclipse Git

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

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

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

--编辑--

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

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

* text eol=crlf

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

Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux.

插图:

考虑现有存储库中有 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