第2章 Gitの設定と関連ファイル
Git は「インストールしたらすぐ使える」ツールですが、
設定ファイルをきちんと整えることで、チーム全体の開発効率が大きく変わります。
この章では、管理者として知っておくべき設定ファイルを順番に見ていきましょう ⚙️
2.1 .gitconfig ─ Gitの設定ファイル
Section titled “2.1 .gitconfig ─ Gitの設定ファイル”Git の設定には 3つの階層 があり、より狭い範囲の設定が優先されます。
flowchart TD
S["🖥️ System
/etc/gitconfig
PC全体に適用"]
G["👤 Global
~/.gitconfig
ユーザー全体に適用"]
L["📁 Local
.git/config
リポジトリごとに適用"]
S --> G --> L
P["⬆️ 下にいくほど優先度が高い"]
style S fill:#F3E5F5,stroke:#8E24AA,color:#4A148C
style G fill:#E3F2FD,stroke:#1E88E5,color:#0D47A1
style L fill:#E8F5E9,stroke:#43A047,color:#1B5E20
style P fill:#FFF9C4,stroke:#F9A825,color:#F57F17
| 階層 | ファイルの場所 | 対象範囲 | 設定コマンド |
|---|---|---|---|
| System | /etc/gitconfig | PC 全体 | git config --system |
| Global | ~/.gitconfig | ユーザー全体 | git config --global |
| Local | .git/config | リポジトリ単位 | git config --local |
最初にやるべき設定
Section titled “最初にやるべき設定”# ユーザー名とメールアドレス(必須)git config --global user.name "Taro Yamada"git config --global user.email "taro@example.com"
# デフォルトブランチ名git config --global init.defaultBranch main
# エディタの設定git config --global core.editor "code --wait" # VS Codegit config --global core.editor "vim" # Vim
# 改行コードの自動変換git config --global core.autocrlf input # macOS / Linuxgit config --global core.autocrlf true # Windows管理者向けおすすめ設定
Section titled “管理者向けおすすめ設定”# fetch 時に不要なリモートブランチを自動削除git config --global fetch.prune true
# push 時に現在のブランチだけを送る(安全)git config --global push.default current
# pull 時に rebase する(直線的な履歴を保つ)git config --global pull.rebase true
# カラー表示を有効化git config --global color.ui auto
# 日本語ファイル名を正しく表示git config --global core.quotepath false設定の確認と編集
Section titled “設定の確認と編集”# すべての設定を一覧表示(どの階層かも表示)git config --list --show-origin
# 特定の設定を確認git config user.name
# Global 設定ファイルをエディタで直接編集git config --global --edit2.2 .gitignore ─ 追跡しないファイルを指定
Section titled “2.2 .gitignore ─ 追跡しないファイルを指定”.gitignore とは?
Section titled “.gitignore とは?”.gitignore は、Git に 「このファイルは追跡しないで」 と伝える設定ファイルです。
リポジトリのルートに配置し、バージョン管理の対象から除外したいファイルを指定します。
なぜ必要なの?
Section titled “なぜ必要なの?”追跡すべきでないファイルの例を見てみましょう。
| ファイルの種類 | 例 | 理由 |
|---|---|---|
| ビルド成果物 | dist/, build/ | 再生成できる |
| 依存パッケージ | node_modules/, vendor/ | パッケージマネージャで復元できる |
| 環境設定 | .env, .env.local | 機密情報を含む可能性 |
| IDE 設定 | .vscode/, .idea/ | 個人の環境に依存 |
| OS 生成ファイル | .DS_Store, Thumbs.db | 不要なノイズ |
| ログ・キャッシュ | *.log, .cache/ | 一時的なデータ |
書き方の基本
Section titled “書き方の基本”# コメント# ─────────────────────────────# 特定のファイル.env.DS_Store
# 特定の拡張子*.log*.tmp
# 特定のディレクトリ(末尾にスラッシュ)node_modules/dist/build/
# ワイルドカード*.min.js*.min.css
# 否定パターン(特定のファイルだけ追跡する)!important.log
# ディレクトリの深さに関係なくマッチ**/logs/**/*.backupパターンの早見表
Section titled “パターンの早見表”| パターン | マッチするもの | 説明 |
|---|---|---|
*.log | error.log, debug.log | 拡張子でマッチ |
build/ | build/ ディレクトリ全体 | ディレクトリ指定 |
/TODO | ルート直下の TODO のみ | ルート限定 |
doc/*.txt | doc/notes.txt | 1階層だけマッチ |
doc/**/*.pdf | doc/a/b/c.pdf | 全階層マッチ |
!README.md | README.md を除外しない | 否定パターン |
プロジェクト別テンプレート
Section titled “プロジェクト別テンプレート”すでに追跡してしまったファイルを除外する
Section titled “すでに追跡してしまったファイルを除外する”.gitignore に追加しただけでは、すでに追跡中のファイル は除外されません。
キャッシュをクリアする必要があります。
# 特定のファイルを追跡から外すgit rm --cached .env
# ディレクトリ全体を追跡から外すgit rm -r --cached node_modules/
# .gitignore 更新後に全体を再適用git rm -r --cached .git add .git commit -m "chore: .gitignore を更新し追跡対象を修正"2.3 .gitattributes ─ ファイル属性の管理
Section titled “2.3 .gitattributes ─ ファイル属性の管理”.gitattributes とは?
Section titled “.gitattributes とは?”.gitattributes は、ファイルごとに Git の扱い方を指定する 設定ファイルです。
改行コードの統一、差分表示の方法、Git LFS の対象指定などに使います。
改行コードの統一
Section titled “改行コードの統一”Windows(CRLF)と macOS/Linux(LF)が混在するチームでは特に重要です。
# テキストファイルの改行コードを自動正規化* text=auto
# 特定の拡張子を明示的に指定*.js text eol=lf*.ts text eol=lf*.json text eol=lf*.md text eol=lf*.yml text eol=lf*.sh text eol=lf
# バイナリファイルとして扱う(差分を取らない)*.png binary*.jpg binary*.gif binary*.ico binary*.woff2 binary| 設定 | 意味 |
|---|---|
text=auto | Git が自動判定してテキストファイルの改行を正規化 |
text eol=lf | チェックアウト時に LF に統一 |
text eol=crlf | チェックアウト時に CRLF に統一 |
binary | バイナリとして扱う(改行変換・差分表示しない) |
diff の表示方法をカスタマイズ
Section titled “diff の表示方法をカスタマイズ”# ロックファイルの差分を非表示package-lock.json -diffyarn.lock -diff
# 画像の差分を exif で表示(要設定)*.png diff=exifGit LFS の指定
Section titled “Git LFS の指定”大容量ファイルを扱う場合(第3章で詳しく解説)。
*.psd filter=lfs diff=lfs merge=lfs -text*.ai filter=lfs diff=lfs merge=lfs -text*.mp4 filter=lfs diff=lfs merge=lfs -text2.4 .git/hooks ─ ローカル自動化
Section titled “2.4 .git/hooks ─ ローカル自動化”Git Hooks とは?
Section titled “Git Hooks とは?”Git Hooks は、特定のイベント(コミット、プッシュなど)が発生したときに
自動でスクリプトを実行する 仕組みです。
flowchart LR
A["git commit 実行"] --> B["pre-commit
フック発火 🔥"]
B -- "成功 ✅" --> C["コミット完了"]
B -- "失敗 ❌" --> D["コミット中止"]
style A fill:#E3F2FD,stroke:#1E88E5,color:#0D47A1
style B fill:#FFF9C4,stroke:#F9A825,color:#F57F17
style C fill:#E8F5E9,stroke:#43A047,color:#1B5E20
style D fill:#FFEBEE,stroke:#E53935,color:#B71C1C
よく使う Hooks
Section titled “よく使う Hooks”| フック名 | タイミング | 使用例 |
|---|---|---|
pre-commit | コミット前 | リンター実行、フォーマットチェック |
commit-msg | コミットメッセージ入力後 | メッセージ形式のチェック |
pre-push | プッシュ前 | テスト実行 |
post-merge | マージ後 | 依存パッケージの自動インストール |
prepare-commit-msg | メッセージ編集前 | テンプレートの自動挿入 |
実践例:pre-commit でリンターを実行
Section titled “実践例:pre-commit でリンターを実行”#!/bin/shecho "🔍 コミット前チェックを実行中..."
# JavaScript/TypeScript のリントnpx eslint --quiet .if [ $? -ne 0 ]; then echo "❌ ESLint エラーがあります。修正してからコミットしてください。" exit 1fi
echo "✅ チェック通過!"exit 0# 実行権限を付与(忘れがち!)chmod +x .git/hooks/pre-commit実践例:commit-msg でメッセージ形式をチェック
Section titled “実践例:commit-msg でメッセージ形式をチェック”#!/bin/shMSG=$(cat "$1")PATTERN="^(feat|fix|docs|style|refactor|test|chore|perf|ci)(\(.+\))?: .+"
if ! echo "$MSG" | grep -qE "$PATTERN"; then echo "❌ コミットメッセージが Conventional Commits 形式ではありません。" echo " 例: feat(auth): ログイン機能を追加" echo " 例: fix: ヘッダーの表示崩れを修正" exit 1fi
exit 0Husky を使ったチーム共有
Section titled “Husky を使ったチーム共有”# Husky のインストールnpm install --save-dev husky
# Husky の初期化npx husky init
# pre-commit フックを設定echo "npx lint-staged" > .husky/pre-commit.husky/ ディレクトリはリポジトリに含められるため、
チーム全員に同じ Hooks が自動適用 されます。
2.5 Git Aliases ─ コマンドの効率化
Section titled “2.5 Git Aliases ─ コマンドの効率化”よく使うエイリアス
Section titled “よく使うエイリアス”毎回長いコマンドを打つのは非効率です。
エイリアスで日常操作をスピードアップしましょう。
# ──── 基本操作 ────git config --global alias.st "status"git config --global alias.co "checkout"git config --global alias.br "branch"git config --global alias.ci "commit"
# ──── ログ表示 ────git config --global alias.lg "log --oneline --graph --all"git config --global alias.ll "log --oneline -15"git config --global alias.last "log -1 HEAD --stat"
# ──── 差分確認 ────git config --global alias.df "diff"git config --global alias.ds "diff --staged"
# ──── 便利系 ────git config --global alias.unstage "restore --staged"git config --global alias.undo "reset --soft HEAD~1"git config --global alias.amend "commit --amend --no-edit"# 設定前git log --oneline --graph --all
# 設定後git lg| ファイル / 設定 | 場所 | バージョン管理 | 主な用途 |
|---|---|---|---|
.gitconfig | ~/.gitconfig | ❌ | ユーザー個人の設定 |
.gitignore | リポジトリルート | ✅ | 追跡除外ファイルの指定 |
.gitattributes | リポジトリルート | ✅ | 改行コード統一、LFS 指定 |
.git/hooks/ | .git/hooks/ | ❌ | ローカル自動化 |
.husky/ | リポジトリルート | ✅ | Hooks のチーム共有 |
| Git Aliases | .gitconfig 内 | ❌ | コマンドのショートカット |