16
16

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 5 years have passed since last update.

Githubで[Error: Key already in use]と表示された場合の複数リポジトリのSSH鍵の使い分け方

Posted at

前提

検索してもなかなかまとまった情報が出てこなかったため、自分用のメモです。

リポジトリAは設定済み。リポジトリBを作成したがリポジトリAで登録済みの重複した鍵は Error: Key already in use と表示されるのでここからどうやって鍵を設定したかを書きます。

目次

  1. 鍵を作る
  2. Githubに鍵を登録する
  3. ~/.ssh/config ファイルに設定を書く
  4. ローカルにあるcloneしたディレクトリに行き、remoteのurlを書き換える

手順

鍵を作る

  • ssh-keygen -t rsa コマンドでリポジトリB用の鍵を作成。 ファイル名を聞かれるので任意のファイル名を入力する。(今回はtest)
❯ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/ユーザー名/.ssh/id_rsa): /Users/ユーザー名/.ssh/test
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/ユーザー名/.ssh/test.
Your public key has been saved in /Users/ユーザー名/.ssh/test.pub.

Githubに鍵を登録する

  • GitHubで作成した鍵を登録する。任意のリポジトリのページの Settings→Deploy keysAdd deploy key を押下する。
スクリーンショット 2018-09-18 14.59.06.png
  • title にわかりやすいように鍵のファイル名と同じ名前を入力。その後 Keytest.pub の中身を全てコピペし Add key を押下。
スクリーンショット 2018-09-18 15.02.07.png

~/.ssh/config ファイルに設定を書く

  • sudo vi ~/.ssh/config でssh configファイルを編集。(なければ作成)
Host github-任意の名前
  User git
  Port 22
  HostName github.com
  IdentityFile ~/.ssh/test(ssh-keygenで作った鍵のファイル名)
  TCPKeepAlive yes
  IdentitiesOnly yes

こんな感じで記述して保存。

ローカルにあるcloneしたディレクトリに行き、remoteのurlを書き換える

  • git remote -v すると多分 git@github.com と付いてると思うので、ssh configファイルに記述した宛先に書き換える。
❯ git remote -v
origin	git@github.com:ユーザー名/リポジトリ名.git (fetch)
origin	git@github.com:ユーザー名/リポジトリ名.git (push)

❯ git remote set-url origin git@github-任意の名前:ユーザー名/リポジトリ名.git
↓()
❯ git remote set-url origin git@github-flower:SugarShooting
Star/FlowerPhotoScore.git

終了

  • 適当にファイルを編集してaddして、commitしてみて git push できればOK。
  • pushがrejectされた場合、git fetch して git merge した内容をコミットしてから git push すれば解決できる。
16
16
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
16
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?