複数のGitHubアカウントを1台のPCで使い分ける設定方法

更新日:2025/01/24

主な設定手順

  1. サブアカウント用のSSH鍵を生成
cd ~/.ssh
ssh-keygen -f id_rsa_private
メインアカウント id_rsa サブアカウント id_rsa_private .ssh/config 設定 Host github.com → id_rsa Host github.private → id_rsa_private
  1. .ssh/configファイルの設定
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

Host github.private
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_private
  1. gitconfigの設定
# ~/.gitconfig_private
[user]
    name = サブアカウント名
    email = サブアカウントのメール

# ~/.gitconfig に追加
[includeIf "gitdir:~/private/"]
    path = ~/.gitconfig_private
ホームディレクトリ 通常プロジェクト – project1/ – project2/ private/ – private-project1/ – private-project2/
  1. 接続確認
ssh -T github.private
  1. リモートリポジトリの追加
git remote add origin github.private:username/repo.git

この設定により、~/private/ディレクトリ以下では自動的にサブアカウントが使用されます。

https://zenn.dev/taichifukumoto/articles/how-to-use-multiple-github-accounts

GitHub Account 1 VS Code GitHub Account 2

https://tanacio.com/how-to-use-multiple-github-accounts

人気記事ランキング
話題のキーワードから探す