35
33

More than 5 years have passed since last update.

GitHubで複数アカウントを利用する(SSH Keys設定)

Last updated at Posted at 2014-03-09

ケース

プライベート用、仕事用でGitHubのアカウントを分けているが、
同じPC(接続元)からどちらのアカウントも利用したい。

※この場合、1台のPCで公開鍵を作成してGitHubの「SSH Keys」で設定するケースが多いが、同じ公開鍵を複数アカウントでは使うことはできない。

解決方法

接続元に複数のキーペアを作成し、.ssh/configで管理。
GitHub接続時に利用する鍵を指定する。

手順

公開鍵の生成、設定

接続元で必要な分のキーペアを作成し、GitHubのそれぞれのアカウントへ登録しておく。
※このとき、キーの名前を変えて生成する。
例)id_rsa.foo、id_rsa.bar
 →id_rsa.foo/id_rsa.foo.pub、id_rsa.bar/id_rsa.bar.pubというようにキーペアが作成される。

手順はGitHubのリファレンスの通り。
Generating SSH Keys · GitHub Help

.ssh/configの作成、設定

ファイルが存在しない場合は、作成して下記の通りように設定する。
※パーミッション:600

.ssh/config

Host github-foo
 HostName github.com
 User git
 IdentityFile /pathToKey/id_rsa.foo

Host github-bar
 HostName github.com
 User git
 IdentityFile /pathToKey/id_rsa.bar

以上で完了。

GitHub接続時

下記のように指定して接続する。

git clone git@github-foo:userName/repositoryName.git
git clone git@github-bar:userName/repositoryName.git
35
33
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
35
33