Git messed up my files, showing chinese characters in some places

后端 未结 4 914
野的像风
野的像风 2021-02-01 04:44

disclaimer: By Git, I mean \'I\' messed up.

Earlier, I wanted git-gui to show me the diff for which it thinks are binary files.

<
4条回答
  •  隐瞒了意图╮
    2021-02-01 05:24

    I had a similar problem recently. We have a project-wide .gitattributes file at the root level, which includes the lines:-

    * text=auto
    *.sql     text
    

    One of our team was writing SQL code using SQL Management Studio which, unknown to him, was saving the files as UTF-16. He was able to check-in the code to Git without problem, but on check-out the code was translated to the Chinese characters as described by this post.

    A hexdump of the files in question confirmed the issue was indeed the translation of 0x000A to 0x000A0D.

    For us the solution was to convert the files to ASCII using the following:-

    1. Delete the offending file from the working directory
    2. Create a temporary .gitattributes file in the local directory to force git to check-out the file without performing line-ending conversion. e.g. include the line *.sql binary

    3. Check-out the file(s) from Git. You should see that the files have not been translated and have no Chinese characters.

    4. Convert the file to ASCII. We used Notepad++ for this, but it's also possible to use iconv, which is installed as part of Git For Windows. I think UTF-8 would also be an option if the file contains non-ASCII characters - but this was not necessary for our purposes.
    5. Check-in the ASCII version of the file
    6. Delete the local .gitattributes file

提交回复
热议问题