10

【茶包射手筆記】PowerShell 轉存 git 輸出到文字檔,中文變亂碼

 2 years ago
source link: https://blog.darkthread.net/blog/ps-save-git-output-garbled/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【茶包射手筆記】PowerShell 轉存 git 輸出到文字檔,中文變亂碼-黑暗執行緒

問題很簡單。git diff HEAD HEAD~ > diff.txt 將修改差異轉存 diff.txt,在 Cmd DOS 視窗執行沒什麼問題:

Fig1_638140506931137703.png

在 PowerShell 環境執行同樣指令,寫入檔案的中文變亂碼:

Fig2_638140506935091701.png

網路爬文繞了一大圈,驀然回首,答案躺在自家部落格深處 - 【茶包射手日記】PowerShell 串接 EXE 輸出中文變亂碼

標準的 PowerShell 串接 EXE 輸出編碼問題,加上 [Console]::OutputEncoding = [Text.Encoding]::UTF8 即可解決。(問題解析請見舊文,此略)

Fig3_638140506937177740.png

感覺忘記自己查過、解過、做過,會成為我「寫程式寫到八十」路上的絆腳石,鳴~~~

【同場加映】

很多人都說程式設計師馬上要被 ChatGPT 幹掉,不免俗拿這個問題問問神奇海螺。ChatGPT 洋洋灑灑列了一堆做法,但沒包含最關鍵的 [Console]::OutputEncoding。

Fig4_638140506939459722.png

潮水退去,茶包老射手褲子還在(雖已滑到低腰),保住一絲尊嚴。

and has 0 comments

Comments

Be the first to post a comment

Post a comment

Comment
Name Captcha 72 - 24 =

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK