Git cant diff or merge.cs utf-16 encoded file

My friend and I worked on the same .cs file at the same time, and when a conflict with git merging indicates a conflict, but the file is not loaded with the usual "HEAD" "→>" because the .cs files were binary files. So we added a lot of things (text * .cs, etc.) to our .gitattributes file to make git treat it as a text file that didn't work.

That's when we realized that git can distinguish between other .cs files and just not that. The reason for this is that it is unicode encoded, as it contains some Chinese characters.

So how do we make git diff or merge files that are in utf-16 or utf-8 format?

The powerful thing is that if I click, gitlab shows exactly what makes it different. Therefore, I do not understand how git can differ on the server, but not with bash.

+7
git c # utf
source share
1 answer

I had a similar problem with * .rc files for a C ++ project, and it was found that the best way to solve this is to use git smudge and clear the filters to store everything in the repository as utf-8 and then convert to utf-16 when write to the working directory.

Thus, all git performs functions such as diff, merges or something that will work on utf8 text without problems, but your working copy will have utf16, which will be happy for the visual studio.

To configure this, make sure you are using a version of git that has an available icon (the latest versions of msysgit do) and add the following to your ~ / .gitconfig file:

[filter "utf16"] clean = iconv -f utf-16le -t utf-8 smudge = iconv -f utf-8 -t utf-16le required 

Then in the .gitattributes file add:

 *.rc filter=utf16 resource.h filter=utf16 

If you already have existing files in utf16 stored as binary files, you need to delete them from the repository and re-add.

 git rm --cached <names-of-utf16-files> git commit -am "removed utf16 files" git add <names-of-utf16-files> git commit -am "added utf16 files as utf8" 

And now everything should work.

+4
source share

All Articles