👨‍💻

WSL で git-credential-manager を使うときの設定方法の注意

に公開

WSL 上の Git で、GitLab Self-managed (セルフホスト版) のサーバーに clone や push や fetch をかけるとこんな警告が出ます。

warning: failed to probe 'https://gitlab.example.com/' to detect provider
warning: この要求の送信中にエラーが発生しました。
warning: see https://aka.ms/gcm/autodetect for more information.

これは、git-credential-manager が出している警告で、既知の問題なので、ここに書いてある通り

git config --global credential.gitlab.example.com.provider gitlab

を実行すれば解決しま........せんがなー。なんで?

いろいろ調べたり試行錯誤したりした経緯はばっさり省略して結論だけ書くと、
Windows 側の Git (Git for Windows) に設定が必要 でした。

そして、ドキュメントをよぉーっく読んだらそう書いてありました🫠。

Using GCM as a credential helper for a WSL Git installation means that any configuration set in WSL Git is NOT respected by GCM (by default).

WSL 側の Git に、推奨設定に準じて

git config --global credential.helper "/mnt/c/Program\ Files\ \(x86\)/Git\ Credential\ Manager/git-credential-manager.exe"

と設定すると、Windows 側にインストールされている git-credential-manager を起動します。これは、Windows の資格情報マネージャー (Credential Manager) にクレデンシャルを格納するためです。Windows 側なので、Windows 側の git-config 設定や環境変数に従って動作するというわけです。
(ただし、この記載に従って WSLENV を設定すれば、WSL 側の git-config 設定に従うようにできるようです。)

なので、git-config の設定はもちろん、GCM_TRACE などの環境変数も、Windows 側に設定する必要があります。
(または、そういう環境変数それぞれひとつひとつを WSLENV に設定すれば、WSL 側で設定した環境変数で動作するのだと思いますが、試していません。)

Discussion