Git无法使用utf-16编码进行比较或合并.cs文件 [英] Git cant diff or merge .cs file in utf-16 encoding

查看:171
本文介绍了Git无法使用utf-16编码进行比较或合并.cs文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一个朋友在同一时间处理同一个.cs文件,当发生合并冲突时,git指出存在冲突,但文件未加载通常的HEAD>>>内容,因为.cs文件是二进制文件。所以我们添加了很多东西(* .cs文本等等) - 我们的.gitattributes文件让git把它当作一个没有用的文本文件。

那是当我们意识到git可以区别其他.cs文件而不是这个。原因是因为它包含一些中文字符,所以它在unicode编码中。



那么我们如何制作utf-16或utf-8格式的git diff或merge文件呢?

<引人入胜的事情是,如果我推,gitlab显示什么是不同的。所以我没有得到如何git可以在服务器上的差异,但不与bash。

解决方案

我有类似的问题,* .rc c ++项目的文件,并找到解决此问题的最佳方法是使用git的污迹和干净的过滤器以UTF-8格式存储存储库中的所有内容,然后在写入工作目录时转换为utf-16。



通过这种方式,git可以完成比如差异,合并或任何对utf8文本没有任何影响的工作,但是您的工作副本将包含utf16,这将使视觉工作室保持开心。



要配置它,请确保您使用的是具有iconv可用的git版本(最新版本的msysgit),并将以下内容添加到〜/ .gitconfig文件:

  [filterutf16] 
clean = iconv -f utf-16le -t utf-8
smudge = iconv -f utf-8 -t utf-16le
required

然后在.gitattributes文件中添加:



< pre $ *。rc filter = utf16
resource.h filter = utf16

如果您已经将存在于utf16中的现有文件存储为二进制文件,那么您需要将它们从存储库中删除并重新添加。

  git rm --cached< utf16文件名称> 
git commit -am已删除utf16文件
git add< utf16文件名称>
git commit -am将utf16文件添加为utf8

现在一切都应该起作用。

A friend and I were working on the same .cs file at the same time and when there's a merge conflict git points out there's a conflict but the file isnt loaded with the usual "HEAD" ">>>" stuff because the .cs files were binary files. So we added numerous things (*.cs text and so on)- to our .gitattributes file to make git treat it as a text file which didnt work.

Thats when we realized that git could diff other .cs files and just not this one. The reason for that is because its in unicode encoding 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 furstrating thing is that if i push, gitlab shows exactly whats different. So I dont get how git can diff on the server but not with bash.

解决方案

I had a similar problem with *.rc files for a c++ project and found the best way to solve this is to use git's smudge and clean filters to store everything in the repository as utf-8, and then convert to utf-16 when writing to the working directory.

This way everything git does such as diffs, merges or whatever will work on the utf8 text without issue, but your working copy will have utf16, which will keep visual studio happy.

To configure this, make sure you're using a version of git that has iconv available (the most recent 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 your .gitattributes file add:

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

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

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.

这篇关于Git无法使用utf-16编码进行比较或合并.cs文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆