由于mac行结束,git将整个文件视为一行 [英] git sees entire file as one line due to mac line endings

查看:134
本文介绍了由于mac行结束,git将整个文件视为一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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
\在文件结尾没有新行
+<?php ^ Mclass Type {^ M ^ M private $ x; // level ^ M ..etc
\在文件结尾没有新行

换句话说,git将我的文件视为单行,这是由于mac行结束。我看过git hub上的各种帖子,并尝试了一些解决方案,但都没有成功。



我试过的链接:





还有相同的^ M行。 git似乎无法在Windows上处理\r。我必须将整个回购行结尾转换为\r\\\
吗?是否没有解决方案离开mac编码?

更多信息

My .gitattributes文件:

 #设置默认行为,以防用户没有设置core.autocrlf。 
* text = auto

#显式声明文本文件,我们希望将文本文件标准化,并在结帐时将
#转换为本地行尾。
* .php文本
* .inc文本
* .js文本
* .txt文本

#声明始终具有CRLF行尾的文件在结帐。
* .sln text eol = crlf

#表示所有真正二进制文件,不应该被修改。
* .png二进制
* .jpg二进制
* .gif二进制

我运行了这个命令: git config --global core.autocrlf true

解决方案 div>

TL; DR版本



  #rewrite所有分支,所有树,所有提交使用适当的行结束符
git filter-branch --tree-filter'〜/ fix-eol.sh'-d / dev / shm / repo-temp --tag-name-filter cat --prune-empty - --all

#git可能在此过程后发出抱怨,并且目录不清晰。只要做:
git reset --hard

#在您的回购库中删除您的标签和旧树引用,
#远程和本地$ b $上的分支指针b git tag -d v1.1.0
git push。 :refs / original / refs / tags / v1.1.0
etc ...

#重新压缩你的对象..
git gc --aggressive

#some更多清理
git修剪

#注意:这会重写您的原始回购,
#you(和其他人)使用它将不得不运行git克隆
#重新下载整个回购。这是一个破坏性的操作,只需
#(在你的整个repo中标准化所有行结尾)
git push --force

cat〜/ fix-eol.sh

 #!/ bin / bash 
#〜/ fix-eol.sh
#将所有js,css,html和txt文件从DOS转换为UNIX行结尾
find。 -type f -regex。* \.\(js\ | css\ | inc\ | html\ | txt\ | php\)| xargs perl -pi -e's / \r\\\
| \\\
| \r / \\\
/ g'



长版:



我说过了,现在尝试第二种解决方案:



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



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

所以我使用了转换器:将新行格式从Mac转换到Windows p>

它重写整个存储库,现在有适当的行结尾,我认为在 push --force 之后,并且redownloading t他的存储库,一切都会好的。


I am working on Windows platform, with files in Mac ANSI encoding

when I edit a single line of a file, and then do a git diff, this is what I see:

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

In other words, git sees my file as a single line, due to the mac line endings. I have looked at various posts on git hub and got tried a few solutions, but neither worked. I am kind of lost as to what is going on and how to commit my files properly.

Links I have tried:

Still same ^M in lines. git does not seem to handle \r on Windows quite well. Must I convert entire repo line endings to say \r\n? Is there not a solution to leave mac encoding as is?

More info:

My .gitattributes file:

# 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

I ran this command: git config --global core.autocrlf true

解决方案

TL;DR version

#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'

Long version:

I said the heck with it and now trying the 2nd solution described here:

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

normalizing all my text and code containing files to contain \n unix-style line endings, using dos2unix command (dos2unix did not work for mac, ha haha, of course! It's not mac2unix)

so I used the converter here: Converting newline formatting from Mac to Windows

It rewrites entire repository, now with proper line endings, and I think that after push --force and people redownloading the repository, all will be okay.

这篇关于由于mac行结束,git将整个文件视为一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆