Gitでユーザ名とかパスワードを求められた時

新しいパソコンでGitの設定を間違ったり忘れたりしていて、少し戸惑ったのでメモ。

  • git pullでPermission denied
$ git pull
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 keyの登録を忘れていました😇
GitHubでssh接続する手順~公開鍵・秘密鍵の生成から~ - Qiitaを参考に公開鍵・秘密鍵のセットを作って登録します。

  • git pushでユーザ名とパスワードを聞かれる

remote urlでhttpsを使用していました😇
sshを使って接続します。

git remote -v
origin  https://github.com/makicamel/test.git (fetch)
origin  https://github.com/makicamel/test.git (push)

GitHubリポジトリの「Clone or download」ボタンを押した時に「Clone with SSH」と表示されていたら「Use SSH」をクリック、表示されたgit:github.comから始まるアドレスをコピー。

git remote set-url origin git@github.com:makicamel/test.git

git remote -v
origin  git@github.com:makicamel/test.git (fetch)
origin  git@github.com:makicamel/test.git (push)

秘密鍵パスフレーズを設定していました😇
ssh-agentに秘密鍵を登録します。

ssh-add ~/.ssh/id_rsa
  • 参考リンク

ssh-agentを利用して、安全にSSH認証を行う - Qiita

それでは快適なGitライフを!