簡體   English   中英

如何在 WSL(Windows 上的 Ubuntu)上使用 Git 憑證存儲?

[英]How to use Git credential store on WSL (Ubuntu on Windows)?

我嘗試按照以下說明操作: https : //stackoverflow.com/a/40312117/21728基本上是這樣做的:

sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

但是當我做任何網絡操作時,我得到這個錯誤:

** (process:7902): CRITICAL **: could not connect to Secret Service: Cannot autolaunch D-Bus without X11 $DISPLAY

我猜這是合乎邏輯的,因為確實沒有 X11 顯示器。

如何使 Git 憑據緩存在 Windows (WSL) 上的 Ubuntu 上工作?

如果您為 Windows 安裝了 Git,則您的系統上會安裝一個 Windows 集成憑據管理器。

您可以從WSL如發現運行Windows可執行文件在這里

要使用它,您可以運行以下命令(假設您的 git for windows 安裝在 C:\\Program Files\\Git 上)

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager-core.exe"

TL; 博士

我已經創建了一個腳本來為你做這件事 我將它與我的 Chef 編排一起使用。

找到或安裝 git-credential-manager.exe

  1. 打開cmd.exe並調用where git-credential-manager.exe
    • 如果它返回一個路徑,很好。 繼續轉換路徑。
    • 如果沒有...
  2. cmd.exe調用where git.exe
    • 如果沒有返回路徑,下一步就是單獨安裝Credential Manager
    • 如果它確實返回了一條路徑,它將類似於:
    • C:\\Program Files\\Git\\cmd\\git.exe
    • 讓我們刪除最后一個斜杠后的所有內容並像這樣更改它:
    • C:\\Program Files\\Git\\mingw64\\libexec\\git-core\\git-credential-manager.exe
    • 如果存在,那太好了。 繼續轉換路徑。
    • 否則...
  3. Microsoft 的 git repo安裝Credential Manager ,然后再次使用where獲取路徑。

將路徑從 DOS 轉換為 Linux

我們需要:

  1. /mnt/c/替換C:\\
  2. 將斜杠從\\翻轉到/
  3. 使用雙反斜杠\\\\轉義空格(和括號,如果有的話)

所以...

  • "C:\\Program Files\\Git\\mingw64\\libexec\\git-core\\git-credential-manager.exe"變成...
  • "/mnt/c/Program\\\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

我上面的腳本有一個功能可以做到這一點

dos_path_to_linux(){
    sed -e 's?\\?/?g' -e' s?[cC]:?/mnt/c?' <<<"$1"
}

但是,正如@12345ieee 此后評論的那樣,一個wslpath實用程序已添加到WSL 版本 17046 中 值得一試,但我目前無法訪問 Windows 進行驗證。 (請注意,即使在我的鏈接的發行說明中給出了使用說明,但該命令目前似乎不包含使用說明、-h 等)

配置git

  1. bash調用git config --global credential.helper "<converted/path>"

使用 Windows 10 和“WSL”,我創建了一個 ~/.gitconfig 文件,但將 [credential] 部分標簽錯誤地輸入為 [credentials]。 我嘗試運行 git credential fill 然后將其輸出提供給 git credential批准,這可能有效,但我懷疑不是因為它說“用法:git credential [fill|approve|reject]”。 最后,我只是跑了:

$ git config --global credential.helper cache

然后做了一個git pull; 當提示輸入用戶名和密碼時,我像往常一樣輸入它們。 在那之后,它記住了它。 我發現它已將(正確命名的)部分添加到我的 ~/.gitconfig 中:

[credential]
        helper = cache

我編輯了它以提供更長的超時時間:

[credential]
        helper = cache --timeout=144000

現在這一切似乎都運行良好。

alias git=git.exe

將簡單地使用 windows 中的 git.exe 及其配置

到目前為止,所有答案都過於復雜。 而且 git 文檔並沒有真正的幫助,他們喜歡參考資料很多,所以你需要按照 2-3 個鏈接來獲取你需要的信息!

  1. 您不需要將 Windows git.exe與 WSL 一起使用! 使用 linux git sudo apt install git-all (我認為它預裝了 Ubuntu)。
  2. 然后您只需按照https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage 上的說明操作即可。

總結

git 默認支持 2 種方法。 要查看您安裝了哪些方法,請執行以下命令:

git help -a | grep credential

我的結果是:

   credential           Retrieve and store user credentials
   credential-cache     Helper to temporarily store passwords in memory
   credential-store     Helper to store credentials on disk

如何緩存和存儲:

緩存

@selkieTG 在他們的回答中涵蓋了這一點,包括完整性......

git config --global credential.helper "cache --timeout 30000"

將您的密碼/令牌緩存 30,000 秒(8 小時 20 分鍾)

商店

git config --global credential.helper "store"

將純文本密碼/令牌存儲在~/.git-credentials

純文本?! 對於 WSL,我完全可以在這里使用純文本。 我輸入憑據以運行我的 Windows 計算機,然后輸入憑據以登錄 WSL2。 我需要隱藏這些嗎? 不是真的,它在我的開發箱上更方便。

經理核心

如果你真的想使用manager-core你可以在你的Ubuntu 版本中安裝它。 然后使用它。

我最近剛剛更新到 WSL2,在我的情況下,以下內容不起作用:

"/mnt/c/Program\\\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

有效的是以下內容: git config --global credential.helper "/c/Program\\\\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

在我從路徑中刪除/mnt/ ,我收到了“未找到”錯誤。

根據我的調查,在干凈的 Windows 啟動后,在 WSL2 中安裝 Windows 驅動器存在問題,更多詳細信息請點擊此處: https://github.com/microsoft/WSL/issues/4122 : https://github.com/microsoft/WSL/issues/4122在我的情況下,這是最可能的原因.

另一個原因可能是/etc/wsl.conf中根目錄的錯誤配置

無法在 WSL2 和 Debian 上使用git-credential-manager.exe進行此操作。 我總是得到remote: Repository not found. 沒有進一步的錯誤。
相反,我對git-credential-manager-core.exe做了同樣git-credential-manager-core.exe所以我的配置現在是credential.helper=/mnt/c/Program\\ Files/Git/mingw64/libexec/git-core/git-credential-manager-core.exe 這立即奏效,事先在 Windows 上設置了 GitHub 2FA/PAT。

我有以下 git 版本:

  • 窗戶:
    git 版本 2.31.0.windows.1
    Windows 版 Git 憑證管理器 v1.20.0.0
  • Debian/WSL2
    版本 2.30.2

下載:(gcmcore-linux_amd64.2.0.567.18224.deb) https://github.com/GitCredentialManager/git-credential-manager/releases/tag/v2.0.567

安裝:

sudo apt install gcmcore -y 或

須藤 dpkg -i <path-to-package.deb> (gcmcore-linux_amd64.2.0.567.18224.deb)

配置:

導出 DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1

git-credential-manager-core 配置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM