🔥

複数 GitHub アカウントの切り替えが面倒? includeIf で自動化する方法

に公開

はじめに

複数の GitHub アカウントを使い分けている方は多いと思います。例えば:

  • 個人用アカウント
  • 会社用アカウント
  • 複数の業務委託先のアカウント

従来は gh auth switch でアカウントを切り替える必要がありましたが、Git の条件付き設定(includeIf)と credential helper を組み合わせることで、ディレクトリごとに自動的にアカウントを切り替えることができます。

従来の方法と問題点

従来の方法

# 個人プロジェクトで作業
cd ~/repos/personal/my-project
gh auth switch --user personal-user
git pull

# 会社プロジェクトで作業
cd ~/repos/work/company-project
gh auth switch --user work-user
git pull

問題点

  • ディレクトリを移動するたびに gh auth switch を実行する必要がある
  • 切り替えを忘れると、誤ったアカウントでコミット・プッシュしてしまう
  • 複数のターミナルを開いている場合、管理が煩雑

解決方法の概要

以下の2つの機能を組み合わせます:

  1. Git の includeIf: ディレクトリごとに異なる設定ファイルを読み込む
  2. credential helper (store): アカウントごとに異なる認証情報ファイルを使用

これにより、ディレクトリに cd するだけで自動的に適切なアカウントが使われます。

前提条件

  • Git がインストールされている
  • GitHub CLI (gh) がインストールされている
  • HTTPS で Git 操作を行う
    • この方法は HTTPS 専用です。SSH 認証では利用できません

設定手順

1. 認証トークンの取得

各アカウントで gh auth login を実行し、トークンを取得します。

# 個人アカウントでログイン
gh auth login

# 会社アカウントでログイン(追加)
gh auth login

# 両方のアカウントが登録されているか確認
gh auth status

出力例:

github.com
  ✓ Logged in to github.com account personal-user (keyring)
  - Active account: true
  - Git operations protocol: https
  - Token: gho_************************************

  ✓ Logged in to github.com account work-user (keyring)
  - Active account: false
  - Git operations protocol: https
  - Token: gho_************************************

2. 各アカウントの認証情報ファイルを作成

# 個人アカウントのトークンを取得
gh auth switch --user personal-user
PERSONAL_TOKEN=$(gh auth token)

# 会社アカウントのトークンを取得
gh auth switch --user work-user
WORK_TOKEN=$(gh auth token)

# 個人用の認証情報ファイルを作成
echo "https://personal-user:${PERSONAL_TOKEN}@github.com" > ~/.git-credentials-personal
chmod 600 ~/.git-credentials-personal

# 会社用の認証情報ファイルを作成
echo "https://work-user:${WORK_TOKEN}@github.com" > ~/.git-credentials-work
chmod 600 ~/.git-credentials-work

3. Git 設定ファイルの作成

~/.gitconfig (メイン設定)

# デフォルト設定(個人用)
[user]
	name = personal-user
	email = personal@example.com

[github]
	user = personal-user

# 個人用の認証情報を使用
[credential "https://github.com"]
	helper =
	helper = store --file ~/.git-credentials-personal

[credential "https://gist.github.com"]
	helper =
	helper = store --file ~/.git-credentials-personal

[init]
	defaultBranch = main

# 会社プロジェクト用の設定を読み込み
[includeIf "gitdir:/home/username/repos/work/**"]
	path = ~/.gitconfig-work

~/.gitconfig-work (会社用設定)

# 会社プロジェクト用設定
[user]
	name = work-user
	email = work@company.com

[github]
	user = work-user

# 会社用の認証情報を使用
[credential "https://github.com"]
	helper =
	helper = store --file ~/.git-credentials-work

4. 設定のポイント

includeIf のパス指定

  • 絶対パスを使用: ~/repos/work/** ではなく /home/username/repos/work/**
  • 末尾に /** を付ける: サブディレクトリすべてに適用
  • 複数の includeIf も可能: 異なるディレクトリで異なる設定を使い分け

credential helper の helper = について

[credential "https://github.com"]
	helper =
	helper = store --file ~/.git-credentials-work

最初の helper = は、親の設定をクリアするために必要です。これがないと、複数の credential helper が順番に実行されてしまいます。

動作確認

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

# 個人プロジェクトで確認
cd ~/repos/personal/my-project
git config user.email
# 出力: personal@example.com

git config --list --show-origin | grep credential
# 出力: file:/home/username/.gitconfig-personal	credential.https://github.com.helper=store --file ~/.git-credentials-personal

# 会社プロジェクトで確認
cd ~/repos/work/company-project
git config user.email
# 出力: work@company.com

git config --list --show-origin | grep credential
# 出力: file:/home/username/.gitconfig-work	credential.https://github.com.helper=store --file ~/.git-credentials-work

実際に fetch してみる

# 個人プロジェクトで fetch
cd ~/repos/personal/my-project
git fetch
# 個人アカウントで認証される

# 会社プロジェクトで fetch
cd ~/repos/work/company-project
git fetch
# 会社アカウントで認証される

ディレクトリ構成の例

~/repos/
├── personal/           # 個人用(デフォルト設定)
│   ├── my-blog/
│   ├── hobby-project/
│   └── oss-contribution/
└── work/              # 会社用(includeIf で設定上書き)
    ├── company-api/
    ├── company-web/
    └── company-infra/

3つ以上のアカウントを使う場合

同様の方法で、いくつでもアカウントを追加できます。

# ~/.gitconfig

# デフォルト(個人用)
[credential "https://github.com"]
	helper =
	helper = store --file ~/.git-credentials-personal

# 会社A
[includeIf "gitdir:/home/username/repos/company-a/**"]
	path = ~/.gitconfig-company-a

# 会社B
[includeIf "gitdir:/home/username/repos/company-b/**"]
	path = ~/.gitconfig-company-b

# OSS プロジェクト
[includeIf "gitdir:/home/username/repos/oss/**"]
	path = ~/.gitconfig-oss

セキュリティ上の注意点

認証情報ファイルの保護

# パーミッションを確認
ls -la ~/.git-credentials-*
# 600 (所有者のみ読み書き可能) であることを確認

# もし違っていたら修正
chmod 600 ~/.git-credentials-*

トークンの管理

  • トークンは定期的にローテーションする
  • 不要になったトークンは GitHub の設定から削除する
  • バックアップする場合は暗号化する

.gitignore への追加(重要)

グローバルの .gitignore に認証情報ファイルを追加:

echo ".git-credentials-*" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

まとめ

Git の includeIf と credential helper を組み合わせることで:

  • ✅ ディレクトリごとに自動的にアカウントが切り替わる
  • gh auth switch の実行が不要
  • ✅ 誤ったアカウントでの操作を防げる
  • ✅ 複数のアカウントを簡単に管理できる

この設定により、複数の GitHub アカウントを使う際のストレスが大幅に軽減されます。

参考

Discussion