グローバルなgitignoreの設定を大公開!
概要
Git 初学者向けに、Git のグローバルな設定の gitignore (~/.config/git/ignore ファイル)のおすすめ設定を書いてみる。
背景
身近な人に Git/GitHub を勧めているが、初学者故に設定をしらないことがあるので、秘匿せずに設定ファイルを紹介する。みんなハッピー!!
対象読者
- Git 初学者
環境
今回の環境は次である。
$ git --version
git version 2.48.1
本論
gitignore とは
この節は読み飛ばしても構わない。
手短に言うと、Git に「これらのファイルはバージョン管理の対象外です」と伝えるためのもの。
無視したいファイル名やパターン(正規表現)を記述する設定ファイル。
対象の例として、コンパイル後の成果物、ログ、個人設定ファイルなどが挙げられる。
指定する方法はいくつかある。
第一に、ローカル(レポジトリ毎)に設定する方法。
これはリポジトリのルートに .gitignore ファイルを置き、それに記述する。
ちなみに、! を付けた行は無視の否定ができる。
これは特定のレポジトリではグローバルの設定を適用したくない場合などに有効である。
第二に、グローバル(全リポジトリ共通)に設定する方法。
本記事の対象がこれなので以下に詳しく述べる。
グローバルな gitignore
~/.config/git/ignore ファイルに記述する。
ドキュメントとしては Git のドキュメント や GitHub Docs を参照されたし。
具体的には ~/.config/git ディレクトリを作成する。
Windows風に言えば $home/.config/git だろうか。
. から始まるのでGUIで作成する場合は隠しファイルになることを念頭に。
Linux なら次のコマンドが使える。
mkdir -p ~/.config/git
そしたら、そのディレクトリ直下にignoreファイルを作成する。例えば次のコマンド。
touch ~/.config/git/ignore
おすすめの設定
次をおすすめの紹介として紹介する。
#以降は行コメントになるのでコピペで全てを記載しても構わない。
!.env.example は .env.example ファイルを明示的に追跡するという意味であることに注意。
# macOS
.DS_Store
.AppleDouble
.Spotlight-V100
.Trashes
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# WSL
*Zone.Identifier
# エディタ/IDE
.vscode/
# ログ・一時ファイルなど
*.log
*.tmp
*.temp
.gocache/
.gomodcache/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# ビルド成果物・依存関係など
/.pnp
/main
.pnp.js
bin/
node_modules/
tmp/
# 環境変数や機密情報など
.env
!.env.example
*.secret
*.key
*.pem
*.crt
cookies.*
関連の紹介
プロジェクトによっては次のようなサイトが役立つ。
頻繁に使用する言語やフレームワークがあればグローバルに記載しても良いかもしれない。
私の設定
私のテンプレートとして使っている ignore ファイルをここに乗せておく。
これは、Zenn 上にバックアップを残す意味も兼ねている。
Go が含まれるのは私がよく Go を利用するためである。
# --- --- --- 汎用設定 --- --- --- #
# macOS
.DS_Store
.AppleDouble
.Spotlight-V100
.Trashes
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# WSL
*Zone.Identifier
# エディタ/IDE
.vscode/
# ログ・一時ファイルなど
*.log
*.tmp
*.temp
.gocache/
.gomodcache/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# ビルド成果物・依存関係など
/.pnp
/main
.pnp.js
bin/
node_modules/
tmp/
# 環境変数や機密情報など
.env
!.env.example
*.secret
*.key
*.pem
*.crt
cookies.*
# --- --- --- Go --- --- --- #
# https://github.com/github/gitignore/blob/main/Go.gitignore
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Code coverage profiles and other test artifacts
*.out
coverage.*
*.coverprofile
profile.cov
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
go.work.sum
# env file
.env
# --- --- --- プロジェクト毎の設定 --- --- --- #
Discussion