
我的電腦運行 Windows 10,啟用了開發人員模式並安裝了 Linux 子系統。當我切換到 bash 時,我嘗試從 git 運行一個簡單的克隆命令:
git clone https://github.com/PrivateRepo/my-private-repo.git
當然,我已經稍微刪除了此處的 URL,但您已經明白了。最初,我從該命令得到的回應是這樣的:
致命:無法訪問'https://github.com/PrivateRepo/my-private-repo.git/': 伺服器憑證驗證失敗。 CA 檔案:/etc/ssl/certs/ca-certificates.crt CRL 檔案:無
我看到一些 Stack Overflow 的答案談到了這個問題,但說實話,我真的不知道恰當的處理該錯誤的方法。話雖如此,我知道一種不正確的方法,那就是透過執行以下命令來完全規避它:
git config --global http.sslverify false
這似乎有效。我關閉所有 SSL 驗證可能很糟糕,但我需要讓它正常工作,而且這並不是這個問題的重點(我正在討論)。但順便說一句,如果你知道解決這個問題的正確方法,我會洗耳恭聽。
接下來,我重新運行git clone
命令,這次它提示我輸入用戶名和密碼,但最終由於不同的原因而失敗。它看起來是這樣的:
git clone https://github.com/PrivateRepo/my-private-repo.git
Cloning into 'my-private-repo'...
Username for 'https://github.com': myuser
Password for 'https://[email protected]':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/PrivateRepo/my-private-repo.git'
不過,我可以向您保證,我輸入的使用者名稱和密碼都是正確的。我還安裝了 Windows 版 Git,嘗試時一切正常。唯一需要注意的是,它沒有在命令列中提示我輸入密碼,而是打開了一個單獨的視窗來輸入憑證,並且還要求我提供六位數的 2FA 程式碼。然後它克隆得很好。我注意到這樣做會在 Github 中建立一個個人存取權杖。因此,我繼續創建了另一個個人存取令牌,並嘗試在 bash 中手動使用它。基本上不是輸入myuser
我輸入的用戶名myuser:mytoken
git clone https://github.com/PrivateRepo/my-private-repo.git
Cloning into 'my-private-repo'...
Username for 'https://github.com': myuser:a1abcdefab2a34567ab8901a2bc3d4567890a1b2
Password for 'https://myuser:[email protected]':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/PrivateRepo/my-private-repo.git'
但正如你所看到的,它仍然失敗了。如何讓 git 在 Windows 上的 bash 中運作?
答案1
您正在目前運行的 Linux 發行版上使用 的實現git
,您可能沒有設定它。嘗試使用git.exe
:
git.exe clone https://github.com/PrivateRepo/my-private-repo.git
您可以alias
在您的.bashrc
:
alias git='git.exe'
答案2
您只需使用您的用戶名,然後使用訪問令牌代替密碼。您不需要密碼,因為令牌會替換它。