为什么 git 认为我的 .sql 文件是二进制文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28145687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:50:09  来源:igfitidea点击:

Why does git think my .sql file is a binary file?

gitgithub

提问by Alan2

I have some .sql files that I just for the first time pushed to github. However when I look at the commit it is saying:

我有一些我第一次推送到 github 的 .sql 文件。但是,当我查看提交时,它说:

BIN ????? WebRole/Sql/Database.sql View
Binary file not shown

Can someone tell me why it's saying "Binary file not shown"

有人可以告诉我为什么说“未显示二进制文件”

回答by VonC

The extension alone isn't enough to GitHub to see if it is a text file.
So it has to look at its content.

仅靠扩展名不足以让 GitHub 判断它是否是文本文件。
所以它必须看它的内容。

And as mentioned in "Why does Git treat this text file as a binary file?", its content might not include enough ascii character to guess it is text file.

并且如“为什么 Git 将此文本文件视为二进制文件?”所述,其内容可能没有包含足够的 ascii 字符来猜测它是文本文件。

You can use a .gitattributes fileto explicitly specify a .sqlshould be a text, not a binary.

您可以使用.gitattributes 文件明确指定.sql应该是文本,而不是二进制文件。

*.sql diff


Update 2018: as I mention in "Utf-8 encoding not working on utf-8 encoded document", Git 2.18 .gitattributes has a new working-tree-encodingattribute.
So, as shown in Rusi's answer:

2018 年更新:正如我在“ Utf-8 编码不适用于 utf-8 编码文档”中提到的,Git 2.18 .gitattributes 有一个新working-tree-encoding属性。
所以,如Rusi回答所示

*.sql text working-tree-encoding=UTF-16LE eol=CRLF


As kostixadds in the comments:

正如kostix评论中添加的那样:

if these files are generated by the Microsoft SQL Management Studio (or whatever it's called in the version of MS SQL Server's management tools you're using), the files it saves are encoded in UCS-2 (or UTF-16) -- a two-byte encoding, which is indeed not text in the eyes of Git

如果这些文件是由 Microsoft SQL Management Studio(或您正在使用的 MS SQL Server 管理工具版本中的任何名称)生成的,则它保存的文件以 UCS-2(或 UTF-16)编码——a二字节编码,在Git眼中确实不是文本

You can see an example in "Git says “Binary files a… and b… differ” on for *.regfiles"

你可以看到一个例子,“混帐说:‘ Binary files a… and b… differ’关于*.reg文件

As mentioned in "Set file as non-binary in git":

如“在 git 中将文件设置为非二进制文件”中所述:

"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

“为什么 Git 将我的文件标记为二进制文件?” 答案是因为它在文件的前 8000 个字符内的某处看到了一个 NUL (0) 字节。
通常,发生这种情况是因为文件被保存为 UTF-8 以外的格式。因此,它可能会保存为 UCS-2、UCS-4、UTF-16 或 UTF-32。所有这些在使用 ASCII 字符时都嵌入了 NUL 字符



As Neomentions in the comments(and in Why does Git treat this text file as a binary file?):

正如Neo在评论中提到(以及为什么 Git 将此文本文件视为二进制文件?):

You can change the encoding of a saved file in SSMS to UTF-8 by selecting encoding 'UTF-8 with signature' from the 'Advanced Save Options' menu item in the File menu.

您可以通过从“文件”菜单的“高级保存选项”菜单项中选择“带签名的 UTF-8”编码,将 SSMS 中保存的文件的编码更改为 UTF-8。

回答by Carl

Using the accepted answer from the linked question and a few other comments I came up with this as a solution to the issue, which is working and runs on Win10

使用链接问题中接受的答案和其他一些评论,我想出了这个作为该问题的解决方案,该问题在 Win10 上运行并运行

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
Get-ChildItem -Recurse *.sql | foreach {
    $MyPath = $_.FullName;
    $Contents = Get-Content $MyPath
    [System.IO.File]::WriteAllLines($MyPath, $Contents, $Utf8NoBomEncoding)
}

回答by Rusi

Ths old question has a new answer — git recently grew an option working-tree-encodingprecisely for these reasons. See gitattributesdocs [Make sure your man page matches since this is quite new!]

这个老问题有了新答案——working-tree-encoding正是由于这些原因,git 最近增加了一个选项。请参阅gitattributes文档 [确保您的手册页匹配,因为这是相当新的!]

Find out the encoding of the sql file eg with file

找出 sql 文件的编码,例如 file

If (say) its utf-16 without bom on windows machine then add to your gitattributes file

如果(比如说)它的 utf-16 在 Windows 机器上没有 bom 然后添加到你的 gitattributes 文件

*.sql text working-tree-encoding=UTF-16LE eol=CRLF

If utf-16 little endinan (with bom) make it

如果 utf-16 little endinan (with bom) make it

*.sql text working-tree-encoding=UTF-16 eol=CRLF

回答by Resource

For those struggling with this issue in SSMS for 2008 R2 (yes, still!), you can set the default encoding as follows:

对于那些在 2008 R2 的 SSMS 中遇到此问题的人(是的,仍然如此!),您可以按如下方式设置默认编码:

  • Locate directory C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\SqlWorkbenchProjectItems\Sql
  • 找到目录 C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\SqlWorkbenchProjectItems\Sql

Locations may vary. This is the directory used by the default installation on Windows 7 64-bit.

地点可能会有所不同。这是 Windows 7 64 位默认安装使用的目录。

  • In this location, add (or edit) empty SQL file SQLFile.sql.
  • 在此位置,添加(或编辑)空的 SQL 文件 SQLFile.sql。

This is used as a template for new .SQL files. Save it using the encoding you require (in my case, Windows-1252 with Windows line endings). The arrow to the right of the 'Save' button gives you a choice of encodings.

这用作新 .SQL 文件的模板。使用您需要的编码保存它(在我的情况下,Windows-1252 带有 Windows 行尾)。“保存”按钮右侧的箭头可让您选择编码。

You need to co-ordinate encodings with your development team to avoid git and SSMS hassle.

您需要与开发团队协调编码以避免 git 和 SSMS 麻烦。

回答by iliketocode

Here is a quick workaround that worked for me, using SSMS 2012. Under tools => options => environment => international settings, if you change the language from "English" to "Same as Microsoft Windows" (it may prompt you to restart SSMS for the changes to take effect), it will not use UTF-16 as the default encoding for new files anymore- all newfiles I create have Codepage 1252 (file => advanced save options) now, which is an 8 bit encoding scheme and seems to have no problems with Git Diff

这是一个对我有用的快速解决方法,使用 SSMS 2012。在工具 => 选项 => 环境 => 国际设置下,如果您将语言从“英语”更改为“与 Microsoft Windows 相同”(它可能会提示您重新启动SSMS 以使更改生效),它将不再使用 UTF-16 作为新文件的默认编码 -我创建的所有文件现在都有代码页 1252(文件 => 高级保存选项),这是一种 8 位编码方案并且似乎没有问题Git Diff

回答by Gyromite

The way to resolve this issue is to force the file to use 8-bit encoding. You could run this PowerShell script to change the encoding of all .SQL files in the current directory and its subdirectories.

解决此问题的方法是强制文件使用 8 位编码。您可以运行此 PowerShell 脚本来更改当前目录及其子目录中所有 .SQL 文件的编码。

Get-ChildItem -Recurse *.sql | foreach {
  $FileName = $_.FullName;
  [System.Io.File]::ReadAllText($FileName) | Out-File -FilePath $FileName -Encoding UTF8;
}