0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Windows】SSH認証で複数のGitHubアカウントを切り替える方法

Posted at

1. 鍵を作る(まだ作ってない場合)

main subの部分は自由に変更してください。

powershell
# メインアカウント
ssh-keygen -t ed25519 -f "$HOME\.ssh\id_ed25519_main"

# サブアカウント
ssh-keygen -t ed25519 -f "$HOME\.ssh\id_ed25519_sub"

以前は rsa が多かったが、今は ed25519 が主流

2. 各アカウントの GitHub に公開鍵を登録

Githubにログイン後、Settings > SSH and GPG keys を開き、右上の「New SSH key」ボタンを押します。
Key の欄に、作成された公開鍵(.pubがついているファイル)の中身を貼り付けます。
以下のコマンドで公開鍵の中身を表示できるのでコピーしましょう。

powershell
# mainの公開鍵を表示
Get-Content "$HOME\.ssh\id_ed25519_main.pub"

# subの公開鍵を表示
Get-Content "$HOME\.ssh\id_ed25519_sub.pub"

3. SSH設定ファイル(config)を作成して接続先を定義

~/.ssh/config を編集します。
VSCodeの場合、以下のコマンドで開けます(なければ新規作成されます)。

code $HOME/.ssh/config

開いたら以下のように設定を記述。

~/.ssh/config
# メインアカウント
Host github-main
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_main #メインアカウントの鍵ファイル
  IdentitiesOnly yes

# サブアカウント
Host github-sub
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_sub #サブアカウントの鍵ファイル
  IdentitiesOnly yes

Host の名前は自由。自分がわかりやすい名前を付けましょう。
例: github-work (仕事用) / github-private (趣味用) など。

4. GitHub との接続を確認

# メインアカウントの接続テスト
ssh -T git@github-main

# サブアカウントの接続テスト
ssh -T git@github-sub

初回接続時は以下のように聞かれるので、yes と入力してEnterを押してください。
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Hi [ユーザー名]! You've successfully authenticated, but GitHub does not provide shell access.
のように表示され、それぞれ異なるアカウント名が表示されたら成功です。

5. アカウントの切り替え

アカウントの切り替えには 「通信先の切り替え」「署名の切り替え」 の2つの設定が必要です。

  • 認証の設定 (git remote): SSH設定(~/.ssh/config)で定義したホスト名(エイリアス)を使い、正しいSSH鍵で通信させるための設定
  • 署名の設定 (git config): そのリポジトリでコミットした際に、誰が書いたコードか(ユーザー名とメアド)を記録するための設定

① リモートURLの設定(認証の切り替え)

ここで「どのアカウント(SSH鍵)としてGitHubに接続するか」を決定します。

先ほど ~/.ssh/config で設定した Host名(エイリアス) を使ってリモートURLを指定する必要があります。これにより、自動的に対応するSSH鍵を選んで接続します。

新規にリポジトリをクローンする場合:

# git clone git@【ssh/configのHost名】:GitHubのユーザー名/リポジトリ名.git
git clone git@github-sub:SubName/my-repo.git

手元のリポジトリをGitHubに連携する場合 (git remote add)

git remote add origin git@github-sub:SubName/my-repo.git

既存のリポジトリの設定を変更する場合 (git remote):

# 現在の設定を確認
git remote -v

# URLをサブアカウント用(エイリアス使用)に書き換え
git remote set-url origin git@github-sub:SubName/my-repo.git

ポイント: @の後ろを configで設定したホスト名(例: github-sub)にする。

② 署名の設定(コミット者情報の切り替え)

ここで「誰がコミットしたか(Gitのログに残る名前)」を決定します。

SSHの接続先をサブアカウントに変えても、Gitのユーザー設定を変えなければ、メインアカウントの名前でコミット履歴が残ってしまいます。

設定コマンド (git config)

# ユーザー名とメールアドレスを設定
git config --global user.name "Sub Account Name"
git config --global user.email "sub_email@example.com"

--global はPC全体のデフォルトを変更します。サブアカウントに切り替えたまま戻し忘れると、メインのプロジェクトでもサブ名義でコミットされてしまうので注意しましょう。
絶対に間違えたくないメインのリポジトリでは、そのフォルダ内で git config user.name、email "..." (--globalなし)を実行しておくと、Global設定に関わらずその値が優先して使わるので安心です。

確認

git config user.name
git config user.email

ただ、毎回打つのも手間なのでPowerShellのプロファイル機能を使って切り替えを自動化してしまいましょう
これにより、gitmaingitsub と入力するだけで、瞬時に署名設定(コミット者情報)を切り替えられるようになります。

  1. PowerShellを起動し、以下のコマンドを実行して設定ファイルをVS Code(またはメモ帳)で開きます

    # VSCodeで開く場合
    code $PROFILE
    
    # メモ帳で開く場合
    notepad $PROFILE
    

    ※ ファイルが存在しない旨のエラーが出た場合は、先に New-Item -Type File -Force $PROFILE を実行してください。

  2. 開いたファイルに以下のコードを追記して保存します

    Microsoft.PowerShell_profile.ps1
    # === Git profile helper for PowerShell ===
    function gitmain {
      git config --global user.name  "Main Account Name" # アカウント名
      git config --global user.email "main@example.com" # メールアドレス
    
      $CurrentName = git config --global user.name
      $CurrentEmail = git config --global user.email
    
      Write-Host "Global git user set to: $CurrentName <$CurrentEmail>"
    }
    
    function gitsub {
      git config --global user.name  "Sub Account Name"
      git config --global user.email "sub@example.com"
    
      $CurrentName = git config --global user.name
      $CurrentEmail = git config --global user.email
    
      Write-Host "Global git user set to: $CurrentName <$CurrentEmail>"
    }
    # === end ===
    

  3. PowerShellを再起動し設定を反映させます

これで、少しコマンドを打つだけで署名の設定を切り替えられるようになりました。

gitmain
# 出力: Global git user set to: Main Account Name <main@example.com>

gitsub
# 出力: Global git user set to: Sub Account Name <sub@example.com>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?