ベルマークのない自由帳

ベルマークのない自由帳

有益と無益の境界例

GitHubを使おうとしたらPermission denied (publickey).と言われてしまった

久々にGit Hubをつかってみようと思ったら

 
git@github.com: Permission denied (publickey).
 
と言われてしまった。
 
 

色々試してみる

Git Hubのほうで「Settings > SSH and GPG keys > New SSH key」にSSH keyを追加し直してみるとSSH接続はできた。
$ pbcopy < ~/.ssh/id_rsa.pub
Enter passphrase for key ‘ /Users/hoge/.ssh/id_rsa’: 
PTY allocation request failed on channel 0 
Hi hoge! You've successfully authenticated, but GitHub does not provide shell access. Connection to github.com closed.
 
早速pushしようとすると
$ git push origin master
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository. 
 
Please make sure you have the correct access rights and the repository exists.
 
「リモートリポジトリが読み取れん。ちゃんとアクセス権持ってる?リポジトリも存在してる?」
と言われて正しいアクセス権ってなんやねん状態。
 
調べてみると` ssh-add `という命令を頻繁に目にすることに気づき、それを調べてみる。


 
要は「ssh-agentというSSHの鍵を保管する場所に鍵を追加するコマンド」らしい。
 
とりあえず実行してみる。
$ ssh-add ~/.ssh/id_rsa.pub
Enter passphrase for /Users/hoge/.ssh/id_rsa:
Identity added: /Users/hoge/.ssh/id_rsa_ (/Users/hoge/.ssh/id_rsa)
 
Identityが追加されたらしいので、もう1回pushしてみる。
$ git push origin master
Counting objects: 65, done. 
Delta compression using up to 4 threads. 
Compressing objects: 100% (60/60), done. 
Writing objects: 100% (65/65), 424.03 KiB | 0 bytes/s, done.
...
 
できた!
(よくわからないけど!)
 
まとめ
Git Hub(Git)使おうとして
公開鍵の権限がない(Permission Denied (publickey))
とか
リモートリポジトリが読み取れない(Could not read from remote repository)
とか言われた場合、
 
SSH KeyをGit Hubに登録しているか確認
SSH Keyをssh-agentにaddしてあげる(ssh-add)
 
と解決するかもしれない。