在git中将文件设置为非二进制文件 [英] Set file as non-binary in git

查看:130
本文介绍了在git中将文件设置为非二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多.cs文件的文件夹。其中一些文件(出于某种原因)被跟踪为二进制文件,并且 git diff 命令无法正常工作。
我尝试将所有这些文件重新保存为UTF-8编码,但没有帮助。我试着改变目录,目录名,文件名和文件扩展名,这些都有所帮助。

I have a folder with lots of .cs files. Some of these files (for some reason) are tracked as binary and the git diff command doesn't work normally with them. I tried re-saving all these files to UTF-8 encoding, but it didn't help. I tried changing the directory, directory name, filename and file extension and all of these helped.

我也尝试修改.gitattributes文件,将* .cs文件视为非-binary但它没有帮助我:

I also tried modifying the .gitattributes file to treat *.cs files as non-binary but it didn't help me:

*.cs diff=csharp

我需要一种方法将所有这些文件设置为非二进制文件,而不用更改它们的路径或名称。有没有这样的方法?

I need a way to set all these files as non-binary w/o changing their path or name. Is there such a way?

推荐答案

你可以这样做来强制git认为它是文本:

You can do this to force git to think it's text:

 *.cs diff

你需要确定它实际上是文本。 强迫Git认为你的文件是文本的时候,实际上不是这样会在各种情况下导致非常不好的行为。 em>

您可能还需要设置其他一些属性:

You may need to set a couple of other attributes too:

 *.cs diff merge text

该文本对于EOL标准化非常有用。您可能需要合并,如果Git仍然认为这些文件在合并时是二进制的。

The text is useful for EOL normalization. You might need merge if Git still thinks the files are binary at merge time.

然而,真正的问题是为什么Git将我的文件标记为二进制文件? 答案是因为它在文件的前8000个字符内有一个NUL(0)字节。通常情况下,这是因为文件被保存为UTF-8之外的其他文件。所以,它可能被保存为UCS-2,UCS-4,UTF-16或UTF-32。所有这些在使用ASCII字符时都嵌入了NUL字符。因此,虽然您的问题表示您已将文件重新保存为UTF-8,但您可能需要再次使用十六进制编辑器进行检查。我怀疑他们不是UTF-8,这是问题的核心。

However, the real question is "Why is Git marking my file as binary?" The answer is because it's seeing a NUL (0) byte somewhere within the first 8000 characters of the file. Typically, that happens because the file is being saved as something other than UTF-8. So, it's likely being saved as UCS-2, UCS-4, UTF-16, or UTF-32. All of those have embedded NUL characters when using ASCII characters. So, while your question says you did re-saved the files as UTF-8, you may want to check again with a hex editor. I suspect that they are not UTF-8, and that's the core of the problem.

这篇关于在git中将文件设置为非二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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