为什么 git 认为我的 .sql 文件是二进制文件?
Why does git think my .sql file is a binary file?
我有一些 .sql 文件,这是我第一次推送到 github。但是,当我查看提交时,它说:
BIN WebRole/Sql/Database.sql View
Binary file not shown
谁能告诉我为什么说 "Binary file not shown"
单独的扩展名不足以 GitHub 来查看它是否是一个文本文件。
所以它必须看它的内容。
并且如“Why does Git treat this text file as a binary file?”中所述,其内容可能不包含足够的 ascii 字符以猜测它是文本文件。
您可以使用 .gitattributes file 明确指定 .sql
应该是文本,而不是二进制文件。
*.sql diff
2018 年更新:正如我在“”中提到的,Git 2.18 .gitattributes 有一个新的 working-tree-encoding
属性。
所以,如图Rusi's :
*.sql text working-tree-encoding=UTF-16LE eol=CRLF
作为kostix adds in :
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
您可以在“Git says “Binary files a… and b… differ
” on for *.reg
files”中查看示例
如“Set file as non-binary in 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
如Neo mentions (and in Why does Git treat this text file as a binary file?):
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.
对于那些在 2008 R2 的 SSMS 中遇到这个问题的人(是的,仍然!),您可以按如下方式设置默认编码:
- 找到目录 C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\VSShell\Common7\IDE\SqlWorkbenchProjectItems\Sql
位置可能会有所不同。这是 Windows 7 64 位上默认安装使用的目录。
- 在此位置,添加(或编辑)空 SQL 文件 SQLFile.sql。
这用作新 .SQL 文件的模板。使用您需要的编码保存它(在我的例子中,Windows-1252 带有 Windows 行尾)。 'Save' 按钮右侧的箭头让您可以选择编码。
您需要与您的开发团队协调编码以避免 git 和 SSMS 麻烦。
这是一个对我有用的快速解决方法,使用 SSMS 2012。在工具 => 选项 => 环境 => 国际设置下,如果您将语言从 "English" 更改为 "Same as Microsoft Windows"(它可能会提示您重新启动 SSMS 以使更改生效),它将不再使用 UTF-16 作为新文件的默认编码 - 我创建的所有 new 文件都有代码页 1252( file => advanced save options) 现在,这是一个 8 位编码方案并且似乎没有问题 Git Diff
解决此问题的方法是强制文件使用 8 位编码。您可以 运行 这个 PowerShell 脚本来更改当前目录及其子目录中所有 .SQL 文件的编码。
Get-ChildItem -Recurse *.sql | foreach {
$FileName = $_.FullName;
[System.Io.File]::ReadAllText($FileName) | Out-File -FilePath $FileName -Encoding UTF8;
}
使用链接问题中接受的答案和其他一些评论,我想出了这个作为问题的解决方案,它在 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)
}
老问题有了新答案 — git 最近增加了一个选项 working-tree-encoding
正是出于这些原因。
请参阅 gitattributes 文档
[确保您的手册页匹配,因为这是全新的!]
找出 sql 文件的编码,例如 file
如果(比如说)它的 utf-16 没有 bom 在 windows 机器上然后添加到你的 git 属性文件
*.sql text working-tree-encoding=UTF-16LE eol=CRLF
if utf-16 little endinan (with bom) make it
*.sql text working-tree-encoding=UTF-16 eol=CRLF
我有一些 .sql 文件,这是我第一次推送到 github。但是,当我查看提交时,它说:
BIN WebRole/Sql/Database.sql View
Binary file not shown
谁能告诉我为什么说 "Binary file not shown"
单独的扩展名不足以 GitHub 来查看它是否是一个文本文件。
所以它必须看它的内容。
并且如“Why does Git treat this text file as a binary file?”中所述,其内容可能不包含足够的 ascii 字符以猜测它是文本文件。
您可以使用 .gitattributes file 明确指定 .sql
应该是文本,而不是二进制文件。
*.sql diff
2018 年更新:正如我在“working-tree-encoding
属性。
所以,如图Rusi's
*.sql text working-tree-encoding=UTF-16LE eol=CRLF
作为kostix adds in
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
您可以在“Git says “Binary files a… and b… differ
” on for *.reg
files”中查看示例
如“Set file as non-binary in 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
如Neo mentions
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.
对于那些在 2008 R2 的 SSMS 中遇到这个问题的人(是的,仍然!),您可以按如下方式设置默认编码:
- 找到目录 C:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\VSShell\Common7\IDE\SqlWorkbenchProjectItems\Sql
位置可能会有所不同。这是 Windows 7 64 位上默认安装使用的目录。
- 在此位置,添加(或编辑)空 SQL 文件 SQLFile.sql。
这用作新 .SQL 文件的模板。使用您需要的编码保存它(在我的例子中,Windows-1252 带有 Windows 行尾)。 'Save' 按钮右侧的箭头让您可以选择编码。
您需要与您的开发团队协调编码以避免 git 和 SSMS 麻烦。
这是一个对我有用的快速解决方法,使用 SSMS 2012。在工具 => 选项 => 环境 => 国际设置下,如果您将语言从 "English" 更改为 "Same as Microsoft Windows"(它可能会提示您重新启动 SSMS 以使更改生效),它将不再使用 UTF-16 作为新文件的默认编码 - 我创建的所有 new 文件都有代码页 1252( file => advanced save options) 现在,这是一个 8 位编码方案并且似乎没有问题 Git Diff
解决此问题的方法是强制文件使用 8 位编码。您可以 运行 这个 PowerShell 脚本来更改当前目录及其子目录中所有 .SQL 文件的编码。
Get-ChildItem -Recurse *.sql | foreach {
$FileName = $_.FullName;
[System.Io.File]::ReadAllText($FileName) | Out-File -FilePath $FileName -Encoding UTF8;
}
使用链接问题中接受的答案和其他一些评论,我想出了这个作为问题的解决方案,它在 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)
}
老问题有了新答案 — git 最近增加了一个选项 working-tree-encoding
正是出于这些原因。
请参阅 gitattributes 文档
[确保您的手册页匹配,因为这是全新的!]
找出 sql 文件的编码,例如 file
如果(比如说)它的 utf-16 没有 bom 在 windows 机器上然后添加到你的 git 属性文件
*.sql text working-tree-encoding=UTF-16LE eol=CRLF
if utf-16 little endinan (with bom) make it
*.sql text working-tree-encoding=UTF-16 eol=CRLF