6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

WindowsのSourceTreeでShift-JISのデータを扱う

Last updated at Posted at 2023-08-03

出来ればUTF-8統一が良いですが、案件ごとに事情が異なるため調査しました。

コミット前のステージングの画面での文字化けを防ぐ場合

SourceTreeのアプリケーションを落としてから開始してください。

.gitattributesファイルをプロジェクト直下に配置します。
文字コードを適用したい拡張子とdiff=の後に文字コードを設定して保存します。

*.txt diff=sjis

git bash から下記の設定を入力します。

git config --global diff.sjis.textconv "iconv -f sjis -t utf-8"

iconvという標準の変換機能を使ってSHIFT-JISファイルを変換します。
※cp932の場合は下記も入力してください。

git config --global diff.cp932.textconv "iconv -f cp932 -t utf-8"

補足

※上記コマンド実行でユーザ配下のgitconfigに設定が記述されます。

[diff "sjis"]
    textconv = iconv -f sjis -t utf-8
[diff "cp932"]
    textconv = iconv -f cp932 -t utf-8

補足2

SourceTree上でファイルをステージしたときには文字化けしなくなるのですが
ステージする前の状態だと化けてしまう可能性があります。
コミットするときは変更を追加しますし、十分差分を見るのには使えるので、困らないとは思います。

コミット後の履歴画面での文字化けを防ぐ場合

プロジェクト直下の.gitattributesに下記を追記します。
別の設定と合わせて追記する場合は半角スペースを空けて末尾に追加してください。

*.txt text working-tree-encoding=sjis diff=sjis

既にShiftJIS のファイルをコミット済みの場合は、.gitattributes を編集した後の場合は
下記のコマンドで一括でSHIFT-JISを修正できます。

git add --renormalize .

補足

working-tree-encodingは実際のコミットしたファイルのエンコードは変わらず
内部的に設定されたエンコードで変換して表示する設定です。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?