4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[TIL] 1つのPCで複数のGitHubアカウントを使用

4
Last updated at Posted at 2022-04-14

私は日本のLIFULL社で働いているエンジニアです。
もし、内容の中で間違った表現や言葉などがあれば、書き込みをしてください。

なぜ必要なのか?

  • 最近、入社して研修を受けながら、個人アカウントと社内アカウントを分離するため頑張っています。
  • GitHubのアカウントはどうすれば良いでしょう?という悩みがあったので、色々調べてみました。
  • SSH Keyを生成してGitHubに適用すると、個人アカウントと社内アカウント分離できることを発見しました。

[STEP 1] SSH Key 生成と登録

1. ssh-keyの生成

  1. ~/.sshフォルダーに移動
    • フォルダーはホームディレクトリーにある
    • もし、なかったら mkdir ~/.sshでフォルダーを生成
    $ cd 
    $ cd ~/.ssh
    
  2. ssh-keyの生成
    • コマンド

      $ ssh-keygen -t {暗号化方式} -b {作成するキーのサイズ} -C "自分のメールアドレス"
      
      • 暗号化方式 : rsa
      • 作成するキーのサイズ : 4096
    • 実コマンドの例

      $ ssh-keygen -t rsa -b 4096 -C "*****@gmail.com"
      
    • コマンドを実行すると下記のように出て、ファイル名を入力

      Generating public/private rsa key pair.
      Enter file in which to save the key (/Users/JeongJaeSoon/.ssh/id_rsa):
      
    • ただ、Enterを押すとid_rsaになるし、そうじゃないと自分が入力した名前がファイル名になる。
      私のPCには個人アカウントはid_rsa_JeongJaeSoonで、社内アカウントはid_rsa_JaeSoonJeong_LIFULLで設定した。

    • その後のパスワードはただEnterを押してパス

      Enter passphrase (empty for no passphrase):
      Enter same passphrase again:
      
    • 生成を成功したら、下記のように出る

      Your identification has been saved in id_rsa_JeongJaeSoon
      Your public key has been saved in id_rsa_JeongJaeSoon.pub
      The key fingerprint is:
      SHA256:sha256sha256sha256sha256sha256 *****@gmail.com
      The key's randomart image is:
      +---[RSA 4096]----+
      |       .+*SO=    |
      |         ..oM=.  |
      |        o. ==+ . |
      |       . Jan. .  |
      |      o G o      |
      |   o o + o .     |
      |  o o = * +      |
      |   o.. = G .     |
      |   oo .   d      |
      +----[SHA256]-----+
      
    • lsで生成結果を確認:1組のファイルが生成されたことが確認可能

      $ ls
      id_rsa_JeongJaeSoon            id_rsa_JeongJaeSoon.pub
      id_rsa_JaeSoonJeong_LIFULL    id_rsa_JaeSoonJeong_LIFULL.pub
      

2. ssh-keyの登録

  1. コマンド
    $ ssh-add {作成したssh-keyのパス}
    
  2. 実コマンドの例
    $ ssh-add ~/.ssh/id_ras_JeongJaeSoon
    
  3. 下記のように出ると成功
    Identity added: ./id_ras_JeongJaeSoon (*****@gmail.com)
    
  4. もし、下記のようにエラーが発生するとeval $(ssh-agent)でssh-agentを再起動
    Could not open a connection to your authentication agent.
    

3. ssh configファイル作成

  1. configファイルはssh-keyを生成した~/.sshに生成
    $ vi ~/.ssh/config
    
  2. configファイルの内容は下記の通り
    • Host github.com-[アカウント名] : アカウント名はよく使われるので覚えやすい言葉にしてください。
    • 自分の環境や欲しい内容を考慮して、作成してください。
    # LIFULL GitHub Account
    Host github.com-lifull
        HostName github.com
        IdentityFile ~/.ssh/id_rsa_JaeSoonJeong_LIFULL
        User JaeSoonJeong
    # Private GitHub Account
    Host github.com-JeongJaeSoon
        HostName github.com
        IdentityFile ~/.ssh/id_rsa_JeongJaeSoon
        User JeongJaeSoon
    

[STEP 2] SSH Key - GitHubアカウント連結設定

1. GitHub アカウントにSSH設定

  • .pubファイルの内容を確認し、GitHubのSSH keysに入力
    $ cat id_rsa_JeongJaeSoon.pub
    ssh-rsa RSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgabRSAgab
    
  • 詳細入力方法はこのドキュメントを参考

2. 設定がきちんとできているか確認

  1. コマンド
    $ ssh -T git@github.com-{3番で指定したgithub.com-[アカウント名]のアカウント名}
    
  2. 実コマンドの例
    $ ssh -T git@github.com-lifull
    $ ssh -T git@github.com-JeongJaeSoon
    
  3. 下記のようにメッセージが出るとyesを入力
    The authenticity of host 'github.com (191.1.128.65)' can't be established.
    RSA key fingerprint is SHA256:sha256sha256sha256sha256sha256sha256sha256sha256
    This key is not known by any other names
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    
  4. 連結が完了すると、成功メッセージが出力
    Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
    Hi JeongJaeSoon! You've successfully authenticated, but GitHub does not provide shell access.
    

[STEP 3] ssh-keyを活用して、GitHubリポジトリに連結

  1. GitHubでgit-testという名前のリモートリポジトリを生成
  2. リポジトリのURLをHTTPSではなくSSHでコピー:git@github.com:JeongJaeSoon/git-test.git
  3. 3番で指定したgithub.com-[アカウント名]アカウント名を追加してgit cloneを実行
    $ git clone git@github.com-JeongJaeSoon:JeongJaeSoon/git-test.git
    
  4. クローンされたローカルリポジトリに移動して、local config アカウントを設定
    $ git config --local user.name "JeongJaeSoon"
    $ git config --local user.mail "wjdwont1006@gmail.com"
    
    • 私の場合はよく使うアカウントが社内アカウントになると思ったので--globalには社内アカウント、
      個人勉強・プロジェクトには個人アカウントを--localに登録しておいた。
4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?