タイトル: 設定の確認
SEOタイトル: git 設定 (config) 確認完全ガイド (global / local / system / includeIf)
| この記事の要点 |
|
基本: 全設定を一覧
# 全設定を表示
git config --list
git config -l # 短縮形
# 出力例
# user.email=foo@example.com
# user.name=Foo Bar
# core.editor=vim
# core.autocrlf=input
# pull.rebase=true
# alias.st=status
3 つのスコープ
Git の設定は 3 階層に保存されます。後者ほど優先 (上書き) されます:
| スコープ | ファイル | 適用範囲 | 優先度 |
|---|---|---|---|
--system | /etc/gitconfig | マシン全ユーザー | 低 |
--global | ~/.gitconfig または ~/.config/git/config | ユーザー全リポジトリ | 中 |
--local | .git/config | このリポジトリのみ | 高 (最優先) |
--worktree | .git/config.worktree | worktree 単位 | 最高 |
# スコープ別に表示
git config --system --list # システム全体
git config --global --list # ユーザー設定
git config --local --list # リポジトリ設定
# 各 .gitconfig を直接見る
cat /etc/gitconfig
cat ~/.gitconfig
cat .git/config
個別の値を取得
# user.name の値
git config --get user.name
git config user.name # --get は省略可
# core.editor
git config --get core.editor
# 該当キーが無ければデフォルト
git config --get user.name || echo "未設定"
# 複数値 (alias 等) を全部
git config --get-all remote.origin.fetch
# 正規表現で部分一致
git config --get-regexp "^user\."
# user.email foo@example.com
# user.name Foo Bar
git config --get-regexp "alias\."
# alias.st status
# alias.co checkout
定義元 (どのファイルか) を表示
「user.email が会社の値になってしまう」「どこで設定されているか分からない」ときに最も有用なオプション:
git config --list --show-origin
# 出力例
# file:/etc/gitconfig core.autocrlf=input
# file:/Users/foo/.gitconfig user.name=Foo Bar
# file:/Users/foo/.gitconfig user.email=personal@example.com
# file:.git/config user.email=work@company.com ← これが最優先
# file:.git/config remote.origin.url=git@github.com:org/repo.git
# スコープも表示
git config --list --show-origin --show-scope
# 個別キーの定義元
git config --show-origin user.email
典型的な確認シナリオ
1. コミット時の名前・メールを確認
git config user.name
git config user.email
# 設定 (リポジトリ単位で仕事用に上書き)
git config --local user.email "work@company.com"
# グローバル (デフォルト)
git config --global user.name "Foo Bar"
git config --global user.email "foo@example.com"
2. push のデフォルト動作
git config push.default
# simple (推奨) / current / matching / upstream
git config pull.rebase
# true (rebase) / false (merge、デフォルト)
git config --global pull.rebase true
3. リモート URL の確認
git config --get remote.origin.url
git remote -v # こちらが一般的
git remote get-url origin
4. エイリアスの確認
git config --get-regexp "^alias\."
# 設定
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all"
Conditional Include (includeIf)
「会社のディレクトリでは仕事用メール、プライベートディレクトリでは個人メール」を 自動切替する仕組み。Git 2.13+ で対応。
# ~/.gitconfig
[user]
name = Foo Bar
email = personal@example.com # デフォルト
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/oss/"]
path = ~/.gitconfig-oss# ~/.gitconfig-work
[user]
email = foo@company.com
signingkey = ABCDEF1234
[commit]
gpgsign = true
これで ~/work/ 配下のリポジトリでは自動的に仕事用設定が適用されます。git config --list --show-origin で確認可能。
主要設定キーの一覧
| キー | 意味 | 例 |
|---|---|---|
user.name | コミットの名前 | Foo Bar |
user.email | コミットのメール | foo@example.com |
user.signingkey | GPG / SSH 署名鍵 | ABCDEF1234 |
core.editor | コミットメッセージのエディタ | vim / code -w |
core.autocrlf | 改行コード変換 | input (Mac/Linux) / true (Windows) |
core.ignorecase | ファイル名大文字小文字 | true (Mac) / false (Linux) |
init.defaultBranch | 初期ブランチ名 | main |
pull.rebase | pull の動作 | true (rebase) |
push.default | push の動作 | simple |
commit.gpgsign | 署名コミット | true |
credential.helper | 認証情報キャッシュ | osxkeychain / store |
設定の追加・変更・削除
# 追加・上書き
git config --global user.name "Foo Bar"
# 削除
git config --global --unset user.name
# 全削除 (スコープ指定)
git config --global --unset-all alias.st
# セクション全部削除
git config --global --remove-section alias
# 編集モード (エディタで開く)
git config --global --edit
git config --local -e
FAQ
Q: コミットすると会社のメールになってしまう
A: git config --list --show-origin user.email で定義元を確認。.git/config (リポジトリ) > ~/.gitconfig (グローバル) の順で上書きされます。個人用リポジトリでは git config --local user.email "personal@example.com"。
Q: --global を付け忘れた
A: リポジトリ単位 (--local) に書かれます。確認は git config --local --list。削除は git config --local --unset key。
Q: 設定ファイルを直接編集していい?
A: OK。~/.gitconfig や .git/config はテキストファイル。ただし構文ミスで Git が動かなくなることがあるので、コマンド経由が無難。
Q: GitHub の SSH 鍵も git config で確認できる?
A: いいえ。SSH 鍵は ~/.ssh/ 配下。cat ~/.ssh/config や ssh -T git@github.com で確認。