GitHub SSH鍵で複数アカウントを切り替える方法
Windows(PowerShell)と Linux/macOS 両対応。1つのPCで複数のGitHubアカウントを扱う場合の設定。
🔑 鍵が1つ(アカウント1つ)の場合
ステップ1:鍵を生成
Linux / macOS:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519
Windows PowerShell:
ssh-keygen -t ed25519 -f $env:USERPROFILE\.ssh\id_ed25519
ステップ2:GitHubに公開鍵を登録
id_ed25519.pub
の中身を GitHub の「Settings → SSH and GPG keys」に貼り付け。
GitHub右上の 丸いプロフィールアイコン をクリック
メニューから「Settings」を選択(これは左寄せ)
② Settings画面 → 左サイドバーの「Access」項目
左サイドバーの中に「Access」カテゴリがあり
→ その中の「SSH and GPG keys」 をクリック
📍このリンクは 左上の縦メニュー にあります(ナビゲーション)
ステップ3:SSH設定ファイルに追記
Linux / macOS:
vi ~/.ssh/config
Windows PowerShell:
notepad $env:USERPROFILE\.ssh\config
中身:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
ステップ4:clone
git clone git@github.com:ユーザー名/リポジトリ名.git
ステップ5:URLを変更したいとき
git remote set-url origin git@github.com:ユーザー名/リポジトリ名.git
🔑🔑 鍵が2つ(複数アカウントの使い分け)
ステップ1:鍵を2つ作成
Linux / macOS:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_main
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work
Windows PowerShell:
ssh-keygen -t ed25519 -f $env:USERPROFILE\.ssh\id_ed25519_main
ssh-keygen -t ed25519 -f $env:USERPROFILE\.ssh\id_ed25519_work
ステップ2:GitHub両方のアカウントに鍵を登録
2つの .pub
をそれぞれのアカウントに登録。
ステップ3:SSH設定を書く
Linux / macOS:
vi ~/.ssh/config
Windows PowerShell:
notepad $env:USERPROFILE\.ssh\config
中身:
Host github-main
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_main
IdentitiesOnly yes
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
ステップ4:cloneコマンド
# メインアカウントのリポジトリ
git clone git@github-main:ユーザー名/リポジトリ名.git
# ワークアカウントのリポジトリ
git clone git@github-work:ユーザー名/リポジトリ名.git
ステップ5:既存リポジトリのURLを切り替える
git remote set-url origin git@github-work:ユーザー名/リポジトリ名.git
※ 鍵の名前やホストエイリアス(github-main など)は自由に設定可能です。
0 件のコメント:
コメントを投稿