由于mac行结尾,git将整个文件视为一行

时间:2014-02-12 15:55:41

标签: git line-endings

我正在使用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 \ n。我必须将整个回购行结尾转换成\ 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

1 个答案:

答案 0 :(得分: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

cat~ / 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/

将包含文件的所有文本和代码规范化以包含\ n unix样式的行结尾,使用dos2unix命令(dos2unix不能用于mac,哈哈哈,当然!这不是mac2unix)

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

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