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

GitGithub

Git Problem Overview


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:

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

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

Git Solutions


Solution 1 - Git

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

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.

You can use a .gitattributes file to explicitly specify a .sql should be a text, not a binary.

*.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-encoding attribute.
So, as shown in Rusi's answer:

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

As kostix adds in the comments:

> 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

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

As mentioned in "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


As Neo mentions in the comments (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.

Solution 2 - Git

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

Find out the encoding of the sql file eg with file

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

*.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

Solution 3 - Git

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

$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)
}

Solution 4 - Git

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

  • Locate directory 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.

  • In this location, add (or edit) empty SQL file 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.

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

Solution 5 - Git

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 new files 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

Solution 6 - Git

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.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlan2View Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitRusiView Answer on Stackoverflow
Solution 3 - GitCarlView Answer on Stackoverflow
Solution 4 - GitResourceView Answer on Stackoverflow
Solution 5 - GitiliketocodeView Answer on Stackoverflow
Solution 6 - GitGyromiteView Answer on Stackoverflow