【发布时间】:2014-02-12 15:55:41
【问题描述】:
我在 Windows 平台上工作,文件采用 Mac ANSI 编码
当我编辑文件的一行,然后执行git diff 时,我看到的是:
diff --git a/class.php b/class.php
@@ -1 +1 @@
-<?php^Mclass Type {^M^M private $x;// level^M ..etc
\ No newline at end of file
+<?php^Mclass Type {^M^M private $x;// level^M ..etc
\ No newline at end of file
换句话说,由于 mac 行结尾,git 将我的文件视为单行。我查看了 git hub 上的各种帖子并尝试了一些解决方案,但都没有奏效。对于发生了什么以及如何正确提交我的文件,我有点迷茫。
我尝试过的链接:
- git-diff to ignore ^M
- https://help.github.com/articles/dealing-with-line-endings#platform-windows
- http://lostechies.com/keithdahlby/2011/04/06/windows-git-tip-hide-carriage-return-in-diff/
在行中仍然是相同的 ^M。 git 在 Windows 上似乎不能很好地处理 \r 。我必须将整个回购行结尾转换为 \r\n 吗?有没有办法让mac编码保持原样?
更多信息:
我的 .gitattributes 文件:
# Set default behaviour, in case users don't have core.autocrlf set.
* text=auto
# Explicitly declare text files we want to always be normalized and converted
# to native line endings on checkout.
*.php text
*.inc text
*.js text
*.txt text
# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf
# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary
我运行了这个命令:git config --global core.autocrlf true
【问题讨论】:
-
MacOS 不再使用该编码(这不是真正的“ANSI”,顺便说一句)。为什么您的文件采用过时的编码?将它们转换为 Windows 编码可能会解决比 git 问题更多的问题。
-
主存储库将在 Linux 系统上运行。我是将行尾转换为 Linux .. 还是仍将它们转换为 Windows?这将帮助我在使用 dos2unix 或 unix2dos 之间做出选择...
-
您是在 Linux、Windows 还是两者上使用文件?
git确实有办法处理 UNIX 风格与 Windows 风格的行尾,但我不熟悉细节。 -
我个人在 Windows 上编辑文件。同事在他的 Mac 上编辑文件。提供文件的 Web 服务器在 Linux 上运行。我们得到了世界上“最好的”。
-
我认为解决方案可能是将所有行尾转换为 Unix 样式 (\n) 并让 git 处理其余部分。 Git 可以在结帐时将行尾转换为本机系统,因此它适用于我(Windows),而 \n 将适用于 Linux/OSX。
标签: git line-endings