2

I wanted to view an old version of a file, so I ran

git show <commit-sha>:example.less > temp.less

to export the old version as temp.less. But the exported file ends up having UTF-16 encoding (even thought example.less is UTF-8 encoded).

I tried --encoding=utf-8 and --encoding=utf8 but neither worked. Besides, I would like to set this in my gitconfig if possible rather than typing it out each time.


Windows 10
Git 2.15.0.windows.1

binaryfunt
  • 6,401
  • 5
  • 37
  • 59
  • The ">" prompt means you're on Windows? Then, which Windows? Which git? There are lot of various gits for Windows. For Win10, try chcp 65001. – ddbug Sep 09 '18 at 21:12
  • Note that Git itself *stores* a raw byte stream. You can access the raw byte stream at any time using `git cat-file -p `. It's then up to you to convert it with whatever tools you like. Using `git show` may, or may not, run blobs through various byte-stream-to-encoding converters, depending on your Git version and other details. (Add Git version and OS details to your question as needed.) – torek Sep 09 '18 at 21:14
  • @torek I've edited the question – binaryfunt Sep 09 '18 at 21:54

2 Answers2

0

First, make sure to use the latest Git for Windows (2.19-rc2), to benefit from the latest bug fixes.

Second, redirect your git show into a file (git show... >afile), and view that file in an UTF-8-capable editor (SublimeText, Notepad++, VSCode, ...)
That will be easier than reading it in an UTF16-CMD (meaning Lucida font + CHCP 65001).

Note: UTF-8 is the default for git show --encoding=....

Scott H adds in the comments:

I had the same problem in PowerShell but then doing it in CMD.exe prompt solved it, even without a special code page (chcp) and without specifying --encoding=utf-8.

Works with both git cat-file blob HEAD:file > retrieved as well as git show ...

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I did try redirecting into a file - the problem is that file ends up with UTF-16 encoding for some reason. I know this because when I try to open the file in Brackets it says it's UTF-16 and can't open it. I've edited the question in case this wasn't clear – binaryfunt Sep 10 '18 at 12:23
  • @binaryfunt just for testing, did you try https://stackoverflow.com/a/10765469/6309? (`Lucida` font + `Chcp 10000` or `chcp 65001`), as shown in https://superuser.com/a/632800/141 – VonC Sep 10 '18 at 12:25
  • @binaryfunt ah: https://stackoverflow.com/a/34899308/6309 do redirect alone is not a good solution. At the very least `CHCP 65001` is needed before the redirection. – VonC Sep 10 '18 at 12:26
  • Tried `> chcp 65001` then git show with and without `--encoding=utf-8`, but it didn't work. Still getting a UTF-16 file. Haven't yet updated git though – binaryfunt Sep 10 '18 at 12:45
  • @bin did you follow stackoverflow.com/a/34899308/6309? – VonC Sep 10 '18 at 12:52
  • Uh, that doesn't seem to be a followable solution, merely a statement that it's not possible – binaryfunt Sep 10 '18 at 13:01
  • @binaryfunt no, I meant: `> chcp 65001` looks like you are trying to redirect a file to `chcp 65001`. That is not what https://superuser.com/a/632800/141 shows. Did you type `chcp 65001` (no `>`), and then try and redirect `git show` to a file? – VonC Sep 10 '18 at 13:06
  • Here `>` at the beginning means the cmd (well, powershell) prompt, I didn't type that – binaryfunt Sep 10 '18 at 13:15
  • @binaryfunt can you try in a regular CMD session (non-Powershell)? – VonC Sep 10 '18 at 13:20
  • I don't see how that will help considering I got the confirmation `Active code page: 65001` after the fact in powershell – binaryfunt Sep 10 '18 at 13:35
  • @binaryfunt Just for testing. – VonC Sep 10 '18 at 13:44
  • @binaryfunt, @VonC, I had the same problem in PowerShell but then doing it in CMD.exe prompt solved it, even without a special code page (chcp) and without specifying `--encoding=utf-8`. Works with both `git cat-file blob HEAD:file > retrieved` as well as `git show ...`. – Scott H Jun 16 '20 at 14:41
  • @ScottH Thank you for your feedback. I have included your comment in the answer for more visibility. – VonC Jun 16 '20 at 15:35
0

This appears to be a problem in Windows PowerShell that goes away by simply using the CMD terminal instead.

I replicated the behavior in Windows 10 with git version 2.27.0.windows.1. In PowerShell I kept getting a file encoded in UTF-16, even trying tips like changing codepage (with chcp 65001) or using --endoding=utf-8.

By simply switching to CMD, the following kinds of commands worked and produced files in UTF-8:

git cat-file blob HEAD:test.txt > retrieved.txt
git show HEAD:test.txt > retrieved.txt

While this doesn't solve the issue within PowerShell, at least it narrows down the problem to just PowerShell.

Scott H
  • 2,644
  • 1
  • 24
  • 28