golangの日記

Go言語を中心にプログラミングについてのブログ

github や gitlab で clone,pull が Permission denied になる問題と解決

default.png


git で clone,pull すると Permission denied が表示される問題と解決

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rightsand the repository exists.


前提として

  • ssh-keygen -t rsa -C "your_email@example.com"SSH鍵を生成
  • github,gitlab のウェブページで鍵を追加している
  • ~/.ssh/config を使っている場合は正しく記述している
  • ssh -T HOST などで以下のように表示され接続はできている
    gitlabであれば Welcome to GitLab @user!
    githubであれば Hi user! You've successfully authenticated...


上記を設定した上で以下のようにエラーになる

$ git pull git@gitlab.com:xxx/xxx.git

git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rightsand the repository exists.

秘密鍵の登録一覧を確認

$ ssh-add -l

追加してみる。

$ ssh-add ~/.ssh/id_rsa_gitlab

解決しました。

$ git pull git@gitlab.com:xxx/xxx.git
Unpacking objects: 100% (10/10), done.

再起動後は、毎回消えるので、そのたびに追加しないとダメなようです。
ただし、MacOS の場合は -K オプションを指定して追加すれば再起動後も追加された状態になるようです。

    $ ssh-add -K ~/.ssh/id_rsa_gitlab