Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

github

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"

like image 871
Alan2 Avatar asked Jan 26 '15 06:01

Alan2


People also ask

Is SQL script a binary file?

Description. Right now, the content of SQL files is not displayed in diffs because they are treated as binary files.

What are SQL binaries?

In SQL, binary data types are used to store any kind of binary data like images, word files, text files, etc. in the table. In binary data types, we have an option like allowing users to store fixed-length or variable length of bytes based on requirements.

How does Git deal with binary files?

How Does Git LFS Work? Git LFS uses pointers instead of the actual files or binary large objects (blobs). So, instead of writing large files/blobs to a Git repository, you write a pointer file, and the files/blobs themselves are written to a separate server.

What is .SQL file format?

A file with . sql extension is a Structured Query Language (SQL) file that contains code to work with relational databases. It is used to write SQL statements for CRUD (Create, Read, Update, and Delete) operations on databases.


2 Answers

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.

like image 166
VonC Avatar answered Oct 05 '22 20:10

VonC


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 
like image 23
Rusi Avatar answered Oct 05 '22 18:10

Rusi