【问题标题】:git sees entire file as one line due to mac line endings由于 mac 行结尾,git 将整个文件视为一行
【发布时间】: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 上的各种帖子并尝试了一些解决方案,但都没有奏效。对于发生了什么以及如何正确提交我的文件,我有点迷茫。

我尝试过的链接:

在行中仍然是相同的 ^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


【解决方案1】:

TL;DR 版本

#rewrite ALL your branches, ALL trees, ALL commits to use proper line endings
git filter-branch --tree-filter '~/fix-eol.sh' -d /dev/shm/repo-temp --tag-name-filter cat  --prune-empty -- --all

#git may complain after this procedure and have unclean directory.  Just do:
git reset --hard

#go over your repo and delete your tags and old tree references, 
#old branch pointers on both remote and local
git tag -d v1.1.0
git push . :refs/original/refs/tags/v1.1.0
etc...

#re-compress your objects..
git gc --aggressive

#some more clean up
git prune

#CAUTION:  this will rewrite your origin repo, 
#you (and everybody else) using it will have to run git clone 
#to re-download the entire repo.  This was a destructive operation afterall
#(normalizing all line endings in your entire repo)
git push --force

猫~/fix-eol.sh

#!/bin/bash
# ~/fix-eol.sh
# convert all js,css,html, and txt files from DOS to UNIX line endings
find . -type f -regex ".*\.\(js\|css\|inc\|html\|txt\|php\)" | xargs perl -pi -e 's/\r\n|\n|\r/\n/g'

长版:

我说了算,现在尝试这里描述的第二种解决方案:

http://blog.gyoshev.net/2013/08/normalizing-line-endings-in-git-repositories/

使用dos2unix命令规范化我所有包含文件的文本和代码以包含\n unix风格的行尾(dos2unix不适用于mac,哈哈,当然!它不是mac2unix)

所以我在这里使用了转换器:Converting newline formatting from Mac to Windows

它重写了整个存储库,现在以正确的行结尾,我认为在push --force 和人们重新下载存储库之后,一切都会好起来的。

【讨论】:

  • 这个命令让我的仓库处于一种奇怪的状态。我收到了这个错误:tree filter branch error: Entry not uptodate. Cannot merge.
  • 确实dos2unix不是mac2unix,但是mac2unix命令也是可以的。或者你在 mac 模式下运行 dos2unix:dos2unix -c mac
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
相关资源
最近更新 更多