0
0

EUC-JP環境下のGitで日本語を扱う

Last updated at Posted at 2024-04-06

背景

UTF-8でないシステムでGitを使おうとすると、日本語が化ける。
不便なので解決したい。

EUC-JPなシステムでの記述です。適宜文字コードを読み替えてください。
Linux環境、git version 2.34.1 で動作確認済み。

結論

git config core.pager "LESSCHARSET=iso8859 less"  # diffとかlogとか(ページャ)
git config core.quotepath false                   # 日本語ファイル名
git config i18n.commitEncoding iso8859            # コミットメッセージ

diffとかlogとか(ページャ)

現象

$ git diff
diff --git a/readme b/readme
index 4472dd6..82a7901 100644
--- a/readme
+++ b/readme
@@ -1,2 +1 @@
 Please read me.
 -<C6><C9><A4><F3><A4><C7><A4><CD>

対策

.gitconfig
[core]
  pager = "LESSCHARSET=iso8859 less"

備考

nkfを使って文字コードを変換する方法もあるけど、これが楽。
あと、開発環境にnkfが入っていなかったり…ね。

日本語ファイル名

現象

$ git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        readme
        "\245\352\241\274\245\311\245\337\241\274"

nothing added to commit but untracked files present (use "git add" to track)

対策

.gitconfig
[core]
  quotepath = false

コミットメッセージ

現象

 $ git commit -m "初期コミット" --allow-empty
 Warning: commit message did not conform to UTF-8.
 You may want to amend it after fixing the message, or set the config
 variable i18n.commitencoding to the encoding your project uses.
 [main (root-commit) 397339b] 遜辿卒端促続促潺奪
 $ git log --oneline
  397339b (HEAD -> main) <C2><BD><C3><A9><C2><B4><C3><BC><C2><A5><C2><B3><C2><A5><DF><A5><C3><A5><C3><88>

対策

.gitconfig
[i18n]
  commitEncoding = iso8859

備考

git log, git show, git blameに使われる i18n.logOutputEncodingっていう設定項目もあるらしい。
設定がなければi18n.commitEncodingと同じ値が使われるようなので、 特段の理由がなければこちらの設定は不要。

余談

コミット時の警告、会社の開発環境で出てたかな…
何年も悩んでようやく辿り着いた答えなのに…

まとめ

リファレンスはちゃんと読もう。

0
0
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
0
0