LoginSignup
4
2

【Git】文字コードがUTF-8以外のファイルを扱う

Last updated at Posted at 2022-09-24

問題

文字コードがUTF-8以外のファイルをGitリポジトリにコミットすると、そのファイルの差分表示が文字化けしてしまう(ファイル自体は文字化けしない)。

解決策

リポジトリ直下に .gitattributes ファイルを作成し、対象のファイルに working-tree-encoding 属性で正しいエンコーディングを指定する。

例えば、WindowsでShift_JISの.txtファイルを扱いたい場合は以下のようにする。

.gitattributes
*.txt working-tree-encoding=cp932

なお、内部的には設定されたエンコーディングとUTF-8とが相互変換されているだけで、実際のGitリポジトリにはUTF-8で上がるので注意。

詳細は公式ドキュメントを参照。
Git - gitattributes Documentation

4
2
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
4
2