wslのvscodeでgithubに接続できない問題とその解決

wslのvscodeでgithubに接続できない問題とその解決

環境

  • win11
  • wsl2(ubuntu22.04)

事象

git pushする際に以下のようなエラーが発生する。

(atcoder-py3.8) ~/AtCoder/ABC > master●● > 0m > git push  Missing or invalid credentials. Error: connect ENOENT /mnt/wslg/runtime-dir/vscode-git-2d573444a9.sock at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) { errno: -2, code: 'ENOENT', syscall: 'connect', address: '/mnt/wslg/runtime-dir/vscode-git-2d573444a9.sock' } Missing or invalid credentials. Error: connect ENOENT /mnt/wslg/runtime-dir/vscode-git-2d573444a9.sock at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1157:16) { errno: -2, code: 'ENOENT', syscall: 'connect', address: '/mnt/wslg/runtime-dir/vscode-git-2d573444a9.sock' } remote: No anonymous write access. fatal: Authentication failed for 'https://github.com/hy-sksem/AtCoder.git/'

解決

  • GitHub CLIをインストールして
  • GitHub CLIの設定

をすると解決可能。

1. GitHub CLIインストール

https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git

以下のコマンドを実行してGitHub CLIをインストールする。

type -p curl >/dev/null || sudo apt install curl -y curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ && sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ && sudo apt update \ && sudo apt install gh -y

2. CLIの設定

cliのインストールが終わったら、gh auth loginを実行する。

gh auth login

色々聞かれるので、以下のように回答していく。

What account do you want to log into? > GitHub.com What is your preferred protocol for Git operations? > HTTPS Authenticate Git with your GitHub credentials? > Yes How would you like to authenticate GitHub CLI? > Login with a web browser

このブラウザが立ち上がらない場合は、https://github.com/login/device に手動でアクセスすることで、認証できる。

20230205-1.png

First copy your one-time code:の後の8桁の英数字を打ち込む。次の画面で承認すると、ターミナルの処理が再開される。

gh auth login  ? What account do you want to log into? GitHub.com ? You're already logged into github.com. Do you want to re-authenticate? Yes ? What is your preferred protocol for Git operations? HTTPS ? Authenticate Git with your GitHub credentials? Yes ? How would you like to authenticate GitHub CLI? Login with a web browser ! First copy your one-time code: xxxx-xxxx Press Enter to open github.com in your browser... ! Failed opening a web browser at https://github.com/login/device exec: "xdg-open,x-www-browser,www-browser,wslview": executable file not found in $PATH Please try entering the URL in your browser manually ✓ Authentication complete. - gh config set -h github.com git_protocol https ✓ Configured git protocol ✓ Logged in as hy-sksem

Logged in as hogehogeがpushしたい自分のアカウントであればOK

あとはやりたかったgit pushなり何なりを実行すれば良い。