1
0

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.

[Mac] VSCodeからGithubにアクセスする

Posted at

最近VSCodeに乗り換えたのですが、ソース管理ビューにて(前もってgit cloneで取得してた)リポジトリをプル/プッシュしようとするとエラーになりました。

...
> git pull --tags 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.

この解決方法をまとめた記事が見当たらなかったので、まとめてみようと思います。

環境

  • macOS Monterey (バージョン 12.0.1)
    • Sierra / High Sierra / Mojave / Catalina / Big Sur も同様?

SSH設定

一番参考になったのは、(Windowsですが)こちらのサイトでした。
VSCodeでGitHub連携のためのSSH設定

Git設定〜GitHub設定は今回の事象特有のものではありません。
よって、ここまでは済んでいるものとします。(済んでない方は申し訳ありませんが他をググって進めてください)

本事象の原因は、git configを確認すると見えてきます。

% git config --list
credential.helper=osxkeychain
...
% git config --show-origin --get credential.helper
file:/Library/Developer/CommandLineTools/usr/share/git-core/gitconfig	osxkeychain

もしかすると、CommandLineTools を入れなければ発生しない問題なのかもしれません。

いよいよ解決方法です。
まず、vi ~/.ssh/configでconfigファイルを作成します。
これに、Macでは以下記述を追加します。

Host github github.com
  User git
  HostName github.com
  AddKeysToAgent yes  # 追加
  UseKeychain yes     # 追加
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes

この設定はMac OS X以降のssh-agent事情を参考にさせていただきました。
設定の意味等は参照先記事にてご確認ください。

これで準備はできましたが、最初の一回だけは手動で認証する必要があります。
ターミナルからgit pull origin masterを実行し、手動でパスフレーズを入力してください。

以後はVSCode(ソース管理ビュー等)からプルできるようになります。

ユーザ設定

VSCodeから初回プッシュした時に以下のエラーメッセージが表示されました。

fatal: no email was given and auto-detection is disabled

VSCodeではgit configを直接設定することはできないんですかね...?
仕方ないので、これもコマンドで設定しました。

% git config --global user.email "YOUR_EMAIL"
% git config --global user.name "YOUR_NAME"

以上、参考になれば幸いです。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?