Skip to content

Instantly share code, notes, and snippets.

@momo-lab
Last active June 25, 2020 22:09
Show Gist options
  • Save momo-lab/976c2bd68467a39ec2cbdb4ec25240a2 to your computer and use it in GitHub Desktop.
Save momo-lab/976c2bd68467a39ec2cbdb4ec25240a2 to your computer and use it in GitHub Desktop.
.NETのソースをgit管理する際にやること

.NETのソースをgit管理する際にやること

gitignoreの設定

gitignore.ioを使うのが楽っぽいので、以下のようにする。

curl -L http://gitignore.io/api/windows,visualstudio > .gitignore

日本語のファイル&フォルダ名

core.quotepathをfalseにすれば、文字化けは発生しない。 (参考: http://dev.classmethod.jp/tool/git/git-avoid-illegal-charactor-tips/

git config --local core.quotepath false

改行コード

gitにはCRLFのまま保存しておくのがよさそうなので、core.autocrlf=falseにする。 ただ、そうすると、diffしたときに行末に「^M」が見えてしまうので、core.whitespace=cr-at-eolも設定する。 (参考: http://deadcode.hatenablog.jp/entry/2014/04/24/152135

git config --local core.autocrlf false
git config --local core.whitespace cr-at-eol

ファイル内の日本語が文字化け(SJIS)

Visual Studioで作成されるファイルのデフォルトエンコーディングがSJISなので、これを文字化けしないようにする。 エンコーディングをUTF-8にできるならそっちの方がよいと思うけど、過去の文化とかあるからなかなか難しい。。。

そんなわけで、以下のように.gitattributeとdiff.*.textconvを設定すればよい。 (参考: http://qiita.com/mather314/items/a6b4bad59e2edd659dd4

.gitattributeというファイルを作成し、以下のように設定。

*.vb diff=sjis
*.cs diff=sjis

んで、iconvでSJISにコード変換する処理を間に挟む。

git config --local diff.sjis.textconv "iconv -f sjis"

結局・・・

以下のコマンドを実行すればOK。

curl -L http://gitignore.io/api/windows,visualstudio > .gitignore
git config --local core.quotepath false
git config --local core.autocrlf false
git config --local core.whitespace cr-at-eol
curl -L https://gist.githubusercontent.com/momo-lab/976c2bd68467a39ec2cbdb4ec25240a2/raw/gitattributes > .gitattributes
git config --local diff.sjis.textconv "iconv -f sjis"

未解決なことも・・・

  • git diffは文字化けしないけど、git add -pのdiffが文字化けしてしまう。
*.vb diff=sjis
*.cs diff=sjis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment