複数のGitHubアカウントを使い分けたい場面ってありますよね。個人用と仕事用、あるいは複数のプロジェクトで別々のアカウントを使いたいとき。今回は実際にハマったトラブルと解決方法を対話形式で紹介します。
発端:Repository not found エラー
生徒:先生、GitHubにリポジトリを作ってpushしようとしたら、エラーが出ました…
user@server:~/project$ git remote add origin git@github.com:myaccount/myrepo.git
user@server:~/project$ git push origin main
Enter passphrase for key '/home/user/.ssh/id_ed25519_github_second':
ERROR: Repository not found.
fatal: Could not read from remote repository.
先生:あー、これはSSHキーの使い分けがうまくいってないね。どういう状況?
生徒:globalにはmyaccount
というアカウントを設定してるんですけど、別のプロジェクトでsecondaccount
を使いたくて…
先生:なるほど。SSHキーのパスフレーズを見ると、secondaccount
用のキーを使おうとしてるけど、実際にはmyaccount
のリポジトリにアクセスしようとしてるから、認証が通らないんだね。
HTTPSだと何故かうまくいく謎
生徒:あ、でもHTTPSでリモートを設定したら、何も聞かれずにpushできました!
user@server:~/project$ git remote set-url origin https://github.com/myaccount/myrepo.git
user@server:~/project$ git push origin main
# すんなり成功!
先生:え、Personal Access Token聞かれなかった?
生徒:全然!何も聞かれませんでした。
先生:それ、おかしいね。調べてみよう。
# 認証情報が保存されているか確認
user@server:~/project$ cat ~/.git-credentials
cat: /home/user/.git-credentials: No such file or directory
user@server:~/project$ git config --global credential.helper
# 何も表示されない
生徒:保存されてないみたいですけど…
犯人はVSCode Remote!
先生:環境変数を確認してみよう。
user@server:~/project$ env | grep -i git
VSCODE_GIT_ASKPASS_NODE=/home/user/.vscode-server/cli/servers/.../node
GIT_ASKPASS=/home/user/.vscode-server/.../askpass.sh
VSCODE_GIT_IPC_HANDLE=/run/user/1000/vscode-git-xxxxxxxx.sock
先生:あー!犯人はVSCode Remoteだ!
生徒:え?どういうことですか?
先生:VSCode Remoteで接続してるでしょ?VSCodeがGitの認証を勝手に管理してくれてるんだ。便利だけど、複数アカウントを使い分けるには問題になる。
生徒:なるほど…じゃあどうすればいいんですか?
解決策:SSH Configでアカウントを使い分ける
先生:SSHの設定ファイルを使って、アカウントごとに別のキーを使うように設定するんだ。まず、今あるSSHキーを確認しよう。
user@server:~/project$ ls -la ~/.ssh/
-rw------- 1 user user 399 Jul 13 id_ed25519_github_first
-rw-r--r-- 1 user user 95 Jul 13 id_ed25519_github_first.pub
-rw------- 1 user user 444 Mar 3 id_ed25519_github_second
-rw-r--r-- 1 user user 94 Mar 3 id_ed25519_github_second.pub
-rw-rw-r-- 1 user user 73 Jul 13 config
生徒:2つのキーがありますね!
先生:そう。今の~/.ssh/config
を見てみよう。
user@server:~/project$ cat ~/.ssh/config
Host github.com
IdentityFile ~/.ssh/id_ed25519_github_second
User git
先生:これだと、全てのGitHub接続がsecond
のキーを使っちゃう。これを修正するよ。
SSH Configの正しい設定方法
先生:~/.ssh/config
をこう書き換えよう。
nano ~/.ssh/config
# 第一アカウント用
Host github.com-first
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_first
IdentitiesOnly yes
# 第二アカウント用
Host github.com-second
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_second
IdentitiesOnly yes
生徒:github.com-first
って何ですか?
先生:これはエイリアス(別名)だよ。実際にはgithub.com
に接続するんだけど、設定上は別の名前をつけることで、使うSSHキーを切り替えられるんだ。
生徒:なるほど!
リモートURLの設定方法
先生:次に、各リポジトリのリモートURLを、このエイリアスを使って設定するんだ。
# 第一アカウントのリポジトリ
cd ~/project-first
git remote set-url origin git@github.com-first:firstaccount/repo.git
# 第二アカウントのリポジトリ
cd ~/project-second
git remote set-url origin git@github.com-second:secondaccount/repo.git
生徒:あ!github.com
じゃなくてgithub.com-first
を使うんですね!
先生:その通り!これで、リポジトリごとに正しいSSHキーが使われるようになる。
動作確認
生徒:やってみます!
# 第一アカウントのリポジトリ
user@server:~/project-first$ git remote -v
origin git@github.com-first:firstaccount/repo.git (fetch)
origin git@github.com-first:firstaccount/repo.git (push)
user@server:~/project-first$ git push origin main
# 成功!
# 第二アカウントのリポジトリ
user@server:~/project-second$ git remote -v
origin git@github.com-second:secondaccount/repo.git (fetch)
origin git@github.com-second:secondaccount/repo.git (push)
user@server:~/project-second$ git push origin main
# こっちも成功!
生徒:できました!ありがとうございます!
次回のPart 2では、.gitignoreが効かない問題と巨大ファイルの削除方法について解説します。