TAKASHI YAMASHINA software engineer

git configの使い方 - グローバルとローカルの設定

git configは、Gitの設定を管理するコマンドです。ユーザー情報、エディタ、エイリアスなどを設定できます。グローバル設定はすべてのリポジトリに適用され、ローカル設定は特定のリポジトリのみに適用されます。

基本的な使い方

設定を追加・変更

bash
# グローバル設定
git config --global user.name "Your Name"

# ローカル設定(現在のリポジトリのみ)
git config --local user.email "your.email@example.com"

# システム設定(すべてのユーザー、管理者権限が必要)
git config --system core.editor vim

設定を確認

bash
# 特定の設定を確認
git config user.name

# すべての設定を表示
git config --list

# グローバル設定のみ表示
git config --global --list

# ローカル設定のみ表示
git config --local --list

設定を削除

bash
# 特定の設定を削除
git config --global --unset user.name

# セクションごと削除
git config --global --remove-section alias

グローバル vs ローカル

グローバル設定(--global)

bash
git config --global user.name "Your Name"

用途:

ローカル設定(--local)

bash
git config --local user.email "work@company.com"

用途:

システム設定(--system)

bash
git config --system core.autocrlf true

主要な設定項目

user: ユーザー情報

bash
# 名前(コミットに記録される)
git config --global user.name "Your Name"

# メールアドレス(コミットに記録される)
git config --global user.email "your.email@example.com"

重要: この情報はすべてのコミットに記録されるため、最初に設定する必要があります。

core: コア設定

bash
# デフォルトエディタ
git config --global core.editor vim
# または
git config --global core.editor "code --wait"  # VS Code

# 改行コードの自動変換(Windows)
git config --global core.autocrlf true

# 改行コードの自動変換(macOS/Linux)
git config --global core.autocrlf input

# ファイルモード(パーミッション)の変更を無視
git config --global core.filemode false

# ページャー(lessなど)
git config --global core.pager less

alias: エイリアス

bash
# よく使うコマンドの短縮形
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

# 複雑なコマンドも登録可能
git config --global alias.lg "log --graph --oneline --all"
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"

使用例:

bash
git co main        # git checkout main
git br             # git branch
git ci -m "msg"    # git commit -m "msg"
git lg             # git log --graph --oneline --all

pull: プル動作

bash
# pullのデフォルト動作(rebaseを使う)
git config --global pull.rebase true

# pullのデフォルト動作(mergeを使う、デフォルト)
git config --global pull.rebase false

# fast-forwardのみ許可
git config --global pull.ff only

push: プッシュ動作

bash
# 現在のブランチと同名のリモートブランチにpush
git config --global push.default simple

# すべてのブランチをpush
git config --global push.default matching

# pushと同時にタグもpush
git config --global push.followTags true

# push時に自動でリモートブランチを作成
git config --global push.autoSetupRemote true

diff: 差分表示

bash
# diffツールを指定
git config --global diff.tool vimdiff

# 移動・リネームを検出
git config --global diff.renames true

# より良い差分アルゴリズム
git config --global diff.algorithm histogram

merge: マージ設定

bash
# mergeツールを指定
git config --global merge.tool vimdiff

# マージ時にfast-forwardを無効化(必ずマージコミットを作る)
git config --global merge.ff false

# コンフリクトスタイル
git config --global merge.conflictstyle diff3

color: 色設定

bash
# カラー表示を有効化(デフォルト)
git config --global color.ui auto

# 常にカラー表示
git config --global color.ui always

# カラー表示を無効化
git config --global color.ui false

# 個別の設定
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto

init: 初期化設定

bash
# デフォルトブランチ名
git config --global init.defaultBranch main

# 古いバージョンではmasterがデフォルト
# mainに変更することを推奨

credential: 認証情報

bash
# 認証情報をキャッシュ(15分間)
git config --global credential.helper cache

# 認証情報をキャッシュ(1時間)
git config --global credential.helper 'cache --timeout=3600'

# macOSのキーチェーンを使用
git config --global credential.helper osxkeychain

# Windowsの資格情報マネージャーを使用
git config --global credential.helper wincred

設定ファイルの直接編集

グローバル設定ファイルを編集

bash
git config --global --edit

エディタが開き、.gitconfigを直接編集できます。

例:

[user]
  name = Your Name
  email = your.email@example.com

[core]
  editor = vim
  autocrlf = input

[alias]
  co = checkout
  br = branch
  ci = commit
  st = status
  lg = log --graph --oneline --all

[pull]
  rebase = true

[push]
  default = simple
  followTags = true

[color]
  ui = auto

[init]
  defaultBranch = main

ローカル設定ファイルを編集

bash
git config --local --edit

.git/configが開きます。

実践的な設定例

初回セットアップ

bash
# ユーザー情報
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# エディタ
git config --global core.editor "code --wait"

# デフォルトブランチ
git config --global init.defaultBranch main

# pullでrebaseを使う
git config --global pull.rebase true

# 基本的なエイリアス
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

プロジェクト別のメールアドレス

bash
# 会社のリポジトリ
cd ~/work/company-project
git config --local user.email "work@company.com"

# 個人のリポジトリ
cd ~/personal/my-project
git config --local user.email "personal@gmail.com"

# グローバルはデフォルトのメールアドレス
git config --global user.email "default@example.com"

エイリアスの活用

bash
# ログ表示
git config --global alias.lg "log --graph --pretty=format:'%C(yellow)%h%Creset -%C(cyan)%d%Creset %s %C(green)(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

# 直前のコミットを修正
git config --global alias.amend "commit --amend --no-edit"

# ステージングを取り消す
git config --global alias.unstage "reset HEAD --"

# 直前のコミットを確認
git config --global alias.last "log -1 HEAD --stat"

# すべてのブランチを表示
git config --global alias.branches "branch -a"

# すべてのエイリアスを表示
git config --global alias.aliases "config --get-regexp ^alias\."

設定の優先順位

Gitは以下の順序で設定を読み込みます(後のものが優先):

  1. システム設定: /etc/gitconfig--system
  2. グローバル設定: ~/.gitconfig--global
  3. ローカル設定: .git/config--local
bash
# すべての設定を表示(優先順位順)
git config --list --show-origin

出力例:

plain
file:/etc/gitconfig     core.autocrlf=true
file:/home/user/.gitconfig      user.name=Your Name
file:/home/user/.gitconfig      user.email=your@example.com
file:.git/config        user.email=work@company.com

この場合、user.emailはローカル設定のwork@company.comが使われます。

条件付き設定

ディレクトリごとに設定を切り替える

[user]
  name = Your Name
  email = personal@gmail.com

[includeIf "gitdir:~/work/"]
  path = ~/.gitconfig-work

[includeIf "gitdir:~/personal/"]
  path = ~/.gitconfig-personal
[user]
  email = work@company.com
[user]
  email = personal@gmail.com

よく使う設定の確認

現在の設定を確認

bash
# ユーザー情報
git config user.name
git config user.email

# エディタ
git config core.editor

# すべての設定
git config --list

# 設定の出所も表示
git config --list --show-origin

# 特定の設定のみ(grep)
git config --list | grep user

設定が適用されているか確認

bash
# 実際に使われる値を確認
git config --get user.email

# すべての設定の最終的な値
git config --list --show-origin | grep user.email

便利な設定

短縮コマンド

bash
# よく使うエイリアス
git config --global alias.s "status -sb"
git config --global alias.aa "add --all"
git config --global alias.cm "commit -m"
git config --global alias.ca "commit --amend"
git config --global alias.po "push origin"
git config --global alias.pu "pull origin"

表示を見やすく

bash
# ログを見やすく
git config --global alias.l "log --oneline --graph --all --decorate"

# ブランチをツリー表示
git config --global alias.tree "log --graph --oneline --all --decorate"

# 最近のコミット
git config --global alias.recent "log -10 --oneline"

作業効率化

bash
# 直前のブランチに戻る
git config --global alias.back "checkout -"

# 現在のブランチ名を表示
git config --global alias.current "branch --show-current"

# 変更を一時退避
git config --global alias.save "stash push -m"

# リモートをpull
git config --global alias.update "pull --rebase"

プラットフォーム別の設定

macOS

bash
# キーチェーンを使用
git config --global credential.helper osxkeychain

# 改行コード
git config --global core.autocrlf input

# ファイル名の大文字小文字を区別
git config --global core.ignorecase false

Windows

bash
# 資格情報マネージャーを使用
git config --global credential.helper wincred

# 改行コードを自動変換
git config --global core.autocrlf true

# 長いパス名をサポート
git config --global core.longpaths true

Linux

bash
# 認証情報をキャッシュ
git config --global credential.helper cache

# 改行コード
git config --global core.autocrlf input

よくある設定ミス

コミット時にユーザー情報がない

bash
# エラー例
fatal: unable to auto-detect email address

# 解決策
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

改行コードの問題(Windows)

bash
# 警告例
warning: LF will be replaced by CRLF

# 解決策
git config --global core.autocrlf true

エディタが設定されていない

bash
# エラー例
error: Terminal is dumb, but EDITOR unset

# 解決策
git config --global core.editor vim
# または
git config --global core.editor "code --wait"

グローバルとローカルの混乱

bash
# 意図しない設定が使われている
git config --list --show-origin | grep user.email

# ローカル設定を削除
git config --local --unset user.email

ベストプラクティス

1. 最初にユーザー情報を設定

bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

すべてのコミットに記録されるため、最優先で設定します。

2. プロジェクトごとにメールアドレスを変える

bash
# 会社のプロジェクト
git config --local user.email "work@company.com"

# 個人のプロジェクト
git config --local user.email "personal@gmail.com"

3. エイリアスを活用

bash
# よく使うコマンドを短縮
git config --global alias.co checkout
git config --global alias.st "status -sb"
git config --global alias.lg "log --graph --oneline --all"

4. 設定ファイルをバージョン管理

bash
# .gitconfigをバックアップ
cp ~/.gitconfig ~/dotfiles/gitconfig

# 新しい環境でリストア
ln -s ~/dotfiles/gitconfig ~/.gitconfig

5. 条件付き設定で自動切り替え

[includeIf "gitdir:~/work/"]
  path = ~/.gitconfig-work

仕事用と個人用で自動的に設定を切り替えます。

よくある質問

Q: グローバルとローカルの違いは?

A:

ローカル設定がグローバル設定を上書きします。

Q: 設定を確認するには?

A:

bash
# すべての設定
git config --list

# 特定の設定
git config user.name

# 出所も表示
git config --list --show-origin

Q: 設定を削除するには?

A:

bash
# 特定の設定を削除
git config --global --unset user.name

# セクションごと削除
git config --global --remove-section alias

Q: エディタが開かないようにするには?

A:

bash
# コミットメッセージを-mで指定
git commit -m "message"

# または、エディタを設定
git config --global core.editor "code --wait"

Q: プロジェクトごとに設定を変えるには?

A:

bash
# プロジェクトのディレクトリで
git config --local user.email "project@example.com"

# または条件付き設定を使う
[includeIf "gitdir:~/projects/"]
  path = ~/.gitconfig-projects

推奨設定テンプレート

基本設定

bash
# ユーザー情報
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# エディタ
git config --global core.editor "code --wait"

# デフォルトブランチ
git config --global init.defaultBranch main

# 改行コード(macOS/Linux)
git config --global core.autocrlf input

# pullでrebase
git config --global pull.rebase true

# pushで上流ブランチを自動設定
git config --global push.autoSetupRemote true

# カラー表示
git config --global color.ui auto

エイリアス

bash
# 基本
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

# ログ
git config --global alias.lg "log --graph --oneline --all"
git config --global alias.l "log --oneline -10"

# 便利
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"
git config --global alias.amend "commit --amend --no-edit"

.gitconfigファイル例

[user]
  name = Your Name
  email = your.email@example.com

[core]
  editor = code --wait
  autocrlf = input

[init]
  defaultBranch = main

[pull]
  rebase = true

[push]
  autoSetupRemote = true
  followTags = true

[alias]
  co = checkout
  br = branch
  ci = commit
  st = status
  lg = log --graph --oneline --all
  unstage = reset HEAD --
  last = log -1 HEAD
  amend = commit --amend --no-edit

[color]
  ui = auto

[credential]
  helper = cache --timeout=3600

まとめ:覚えておくべきコマンド

bash
# 設定の追加・変更
git config --global user.name "Your Name"
git config --local user.email "email@example.com"

# 設定の確認
git config user.name              # 特定の設定
git config --list                 # すべての設定
git config --list --show-origin   # 出所も表示

# 設定の削除
git config --global --unset user.name

# 設定ファイルの編集
git config --global --edit        # グローバル設定
git config --local --edit         # ローカル設定

# エイリアス
git config --global alias.st status
git config --global alias.co checkout

最初に設定すべきもの

bash
# 必須
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 推奨
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global core.editor "code --wait"