LoginSignup
2
0

More than 3 years have passed since last update.

複数のGitHubアカウントを使い分ける

Last updated at Posted at 2019-03-16

概要

複数のGitHubアカウントを使い分ける方法です。

手順

SSH鍵の作成

まず秘密鍵/公開鍵のペアを作成します。
RSAより強固でパフォーマンスも優れているEd25519でSSH鍵を作成します。
(すでに鍵を作成済みならそれを使用しても構いませんが、RSA 1024bitを使っている場合やパスフレーズ設定していない場合は良い機会なので、よりセキュリティの強固なEd25519に変えてみてはどうでしょう)

$ ssh-keygen -t ed25519 -C XXXX@gmail.com

※パスフレーズはちゃんと設定しましょう。(秘密鍵が漏洩してしまった際の対策です)
パスフレーズは10〜30文字で、推測されやすい文は避け、大文字・小文字・数字・記号を混ぜられると良いです。
ssh-keygenのマニュアルには

Good passphrases are 10-30 characters long, are not simple sentences or otherwise easily guessable
(English prose has only 1-2 bits of entropy per character, and provides very bad passphrases), and contain a mix of upper and lowercase letters, numbers, and
non-alphanumeric characters.

とあります。

SSH鍵の指定

direnvの設定

使用しているgitのバージョンが2.3以上であれば、 GIT_SSH_COMMAND が有効なので、gitで使用する秘密鍵を環境変数で指定することができます。
direnvと組み合わせれば、特定のディレクトリ配下でgitが使用する秘密鍵を自動で切り替えることができます。

.envrc
export GIT_SSH_COMMAND='ssh -i ~/.ssh/id_ed25519_xxx'

ssh_configの編集

gitのバージョンが2.3未満の場合は GIT_SSH を使うか、ssh_configを変更する必要があります。

$ vi ~/.ssh/config

Host xxx.github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_ed25519_xxx
  Host github.com

公開鍵をGitHubへ登録

作成した公開鍵をGithubに登録します。

「Settings」-「SSH and GPG keys」で「New SSH key」から登録します。

動作確認

プライベートリポジトリをクローンしてみる。

$ git clone git@github.com:xxx/xxxx.git
Cloning into 'xxxx'...
Enter passphrase for key '/Users/xxx/.ssh/id_ed25519_xxx': パスフレーズを入力
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 26 (delta 1), reused 20 (delta 0), pack-reused 0
Receiving objects: 100% (26/26), 4.45 KiB | 505.00 KiB/s, done.
Resolving deltas: 100% (1/1), done.

無事クローンすることできました。

2
0
2

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
2
0