VS Source Code Files are Binary in Git

I have recently downloaded Visual Studio 2017 (Community, v15.5.6) and found a strange quirk when creating a new project. It wasted a few hours, which always makes something worthy of a walkthrough.

I created a new Visual C++ Windows Console Application, which starts with the following source code files:

  • stdafx.h
  • stdafx.cpp
  • targetver.h
  • ConsoleApplication1.cpp

Then there are the project files:

  • ConsoleApplication1.sln
  • ConsoleApplication1.vcxproj
  • ConsoleApplication1.vcxproj.filters

Now, when you view the source files on your chosen Git tool, it will interpret them as binary and give you no information whatsoever about their text contents. This is, of course, really annoying. What’s even stranger, the project files appear as text in the viewer just as you would expect.

The easiest way I found to troubleshoot and fix it was to open the files in Notepad++. Click on the Encoding menu, and what do you see:

2018-11-10 07_39_30-C__Users_jtr_source_repos_vs-studio-boost-built-starter_stdafx.h - Notepad++.png
Initial source code files are encoded as UTF-16 (shown as UCS-2, Llittle-Endian, with Byte Order Mark here)

The source code has been encoded as UTF-16, which Git cannot deal with.

This can be solved in the same place by simply clicking on “Encode in UTF-8” and saving the file. Git will now recognise it as text.

What elevates this from annoying to downright perplexing is that if you add a new file to your project, it created as UTF-8. With the project files also being UTF-8, it is these four initial files that are the exception, not the rule.

This is a known issue, and will hopefully be fixed soon.


One thought on “VS Source Code Files are Binary in Git

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s