タイトル: gitユーザー名とemailの設定
SEOタイトル: Git ユーザー名/Email 設定完全ガイド
| この記事の要点 |
|
最低限の初期設定
Git をインストールしたら最初にやる設定です:
# 全リポジトリ共通の名前と email
git config --global user.name "Yamada Taro"
git config --global user.email "taro@example.com"
# その他の推奨初期設定
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global core.editor "vim"
git config --global core.autocrlf input # Linux/macOS
git config --global core.autocrlf true # Windows
# 確認
git config --global --list
git config --get user.name
git config --get user.email
スコープ: global / local / system
| スコープ | 保存先 | 対象範囲 |
|---|---|---|
--system | /etc/gitconfig(OS 全体) | 全ユーザの全リポジトリ |
--global | ~/.gitconfig(ユーザ毎) | そのユーザの全リポジトリ |
--local | .git/config(リポ毎、デフォルト) | 当該リポジトリのみ |
--worktree | .git/config.worktree | 多重 worktree 別個(Git 2.20+) |
優先度は local > global > system。リポジトリ内で local が設定されていれば、それが global を上書きします。
# プロジェクト毎に別 email を使う
cd ~/work/company-project
git config --local user.email "taro@company.co.jp"
# 確認(local 優先で表示)
git config user.email # → taro@company.co.jp
git config --global user.email # → taro@example.com(個人用)
GitHub のプライベート Email
GitHub にコミットすると、デフォルトでは設定した email がコミット履歴に露出します。スパム / 個人特定対策で、noreply email が用意されています:
- GitHub Settings → Emails → Keep my email addresses private をオン
- 表示される 12345678+username@users.noreply.github.com をコピー
- Git に設定
git config --global user.email "12345678+username@users.noreply.github.com"
# 既存リポジトリで過去コミットの email も書き換えたい
git config --local user.email "12345678+username@users.noreply.github.com"
git rebase -i --root --exec "git commit --amend --reset-author --no-edit"
# ⚠ 履歴書き換え。共有リポでは禁止
仕事用 / 個人用の自動切替
includeIf 機能を使えば、ディレクトリパスに応じて自動で設定を切り替えられます:
# ~/.gitconfig
[user]
name = Yamada Taro
email = taro@personal.example.com
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/oss/"]
path = ~/.gitconfig-oss# ~/.gitconfig-work
[user]
email = taro@company.co.jp
signingkey = ABCD1234FEDC
[commit]
gpgsign = true# ~/.gitconfig-oss
[user]
email = 12345678+yamada@users.noreply.github.com
これで ~/work/ 配下では会社 email、~/oss/ 配下では GitHub noreply email を自動で使い分けます。
署名付きコミット (GPG / SSH 署名)
コミットを暗号署名することで、GitHub 上に Verified バッジが付きます。なりすまし防止に有効:
GPG 鍵
# GPG 鍵を生成
gpg --full-generate-key
# RSA 4096 / 期限 / 名前 / email を入力
# 鍵 ID を取得
gpg --list-secret-keys --keyid-format=long
# sec rsa4096/ABCD1234EFGH5678 2024-01-01 [SC]
# Git に登録
git config --global user.signingkey ABCD1234EFGH5678
git config --global commit.gpgsign true
git config --global tag.gpgsign true
# GitHub に公開鍵を登録
gpg --armor --export ABCD1234EFGH5678
# 出力を GitHub Settings → SSH and GPG keys → New GPG key に貼り付け
SSH 鍵で署名 (Git 2.34+)
# 既存の SSH 鍵 (~/.ssh/id_ed25519) を流用可能
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true
# allowed_signers ファイル設定(検証用)
echo "taro@example.com $(cat ~/.ssh/id_ed25519.pub)" >> ~/.ssh/allowed_signers
git config --global gpg.ssh.allowedSignersFile ~/.ssh/allowed_signers
# GitHub の SSH キー登録画面で "Signing Key" タイプを選択
確認・修正
# 設定一覧(優先度順、上書き解決済)
git config --list
# 各スコープ個別
git config --global --list
git config --local --list
git config --system --list
# どこで設定されたか追跡
git config --show-origin --get user.email
# file:/home/taro/.gitconfig taro@example.com
# 値の削除
git config --global --unset user.name
git config --local --unset user.email
# 直接ファイル編集
git config --global --edit # ~/.gitconfig を $EDITOR で開く
よくあるトラブル
「Please tell me who you are」エラー
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'taro@DESKTOP.(none)')
name / email が未設定か、リポジトリ固有の設定が必要です。上記コマンドで設定してください。
過去コミットの author を一括変更
# 個人 → 会社 email に書き換え(共有リポでは禁止)
git filter-branch --env-filter '
OLD_EMAIL="taro@personal.example.com"
NEW_NAME="Yamada Taro"
NEW_EMAIL="taro@company.co.jp"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]; then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]; then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
# モダンには git-filter-repo を使う
pip install git-filter-repo
git filter-repo --email-callback '
return email.replace(b"taro@personal.example.com", b"taro@company.co.jp")
'
GitHub で「Unverified」バッジが付く
署名鍵を GitHub に登録していないか、コミット時の email と GitHub アカウントの email が一致していません。GitHub Settings → Emails と、Settings → SSH and GPG keys を確認。
ベストプラクティス
- ~/.gitconfig は git 管理(dotfiles リポ)に入れて複数 PC で共有
- 仕事メールは local または includeIf でディレクトリ毎に設定(個人リポに会社 email が紛れ込むのを防ぐ)
- OSS への commit は GitHub noreply email を使う
- セキュリティ重視なら GPG / SSH 署名を有効化
- 新人オンボーディングで git config --global init.defaultBranch main など合わせて設定
FAQ
Q: 設定した email が GitHub に紐づかない
A: コミットの email と、GitHub アカウントに登録された email が一致する必要あり。GitHub Settings → Emails で複数登録可能。
Q: user.useConfigOnly = true は?
A: user.name / user.email が未設定のときに自動推測(hostname など)させない。会社 PC の (none) 混入防止に有効。
Q: 仕事の commit を間違って個人 email で push してしまった
A: 共有前なら git commit --amend --author="Name で直近修正可能。push 済なら filter-repo で書き換え + force push(事前にチーム合意)。
関連
- git config — 設定操作のメインコマンド
- git commit --amend — 直近 author 修正
- git filter-repo — 履歴書き換え(モダン)
- GitHub Verified Commits — 署名による信頼性向上