How can I make Git show changes to my .sql files?

The only method I found that worked was to change the encoding of .sql files from the default UTF16 encoding to UTF8. The below outlines three approaches based on your circumstances:

For a small number of files

  1. Within Visual Studio or SSMS choose Save As
  2. Choose the drop down arrow beside the save button enter image description here
  3. Choose "Save with encoding..."
  4. Select Yes to the Confirm Save As dialog

enter image description here

  1. Choose Unicode (UTF-8 with signature) - CodePage 65001 from the resulting dialog box and press OK. Note that the UTF-8 option is usually above the currently selected encoding (Unicode - Codepage 1200)

enter image description here

For a large number of files

If you have many files to change and are comfortable with running Powershell scripts you may want to try the following:

  1. Download the powershell script created by Microsoft MCC, MVP Stefan Roth from this link
  2. The powershell script is not digitally signed so you will need to open powershell as an administrator and then downgrade your execution policy (temporarily) outlined here. Choose either the bypass or unrestricted policy.
  3. How to run powershell scripts is detailed here. The path to the script will need quotes around it. The script then requires source and destination folders which must not be surrounded in quotes. The Encoding requested is simply utf8.

For any future files created

In order to prevent having to convert files in the future you can alter the template for the new queries created in SSMS by using the approach outlined in this link


Git usually guesses correctly whether a blob contains text or binary data by examining the beginning of the contents. In your case, however, git is getting confused and treating the file as binary, possibly due to binary data somewhere in the file.

From the git-diff manpage:

   -a, --text
       Treat all files as text.

So you can still get the text diff quite easily as follows:

git diff -a WebRole/Sql/Objects/dbo.Content.Table.sql

To override git incorrect guess add the following to .gitattributes in the same directory as the file:

*.sql diff

and commit this file. This will force git to treat every .sql file as text from now on, wether or not it contains binary data.

Tags:

Git

Github