簡體   English   中英

如何為git pull輸入帶密碼的命令?

[英]How to enter command with password for git pull?

我想在一行中執行此命令:

git pull && [my passphrase]

怎么做?

這不完全是您要求的,但對於 http(s):

  • 您可以將密碼放在 .netrc 文件中(Windows 上為 _netrc)。 從那里它會被自動拾取。 它將以 600 權限轉到您的主文件夾。
  • 您也可以使用https://user:pass@domain/repo克隆回購協議,但這並不是真正推薦的,因為它會在很多地方顯示您的用戶/通行證...
  • 一個新的選擇是使用憑證助手 請注意,憑據將使用標准憑據幫助程序以明文形式存儲在您的本地配置中。 credential-helper with wincred 也可以在 Windows 上使用。

憑據助手的使用示例

  • git config credential.helper store - 無限期地存儲憑據。
  • git config credential.helper 'cache --timeout=3600' - 存儲 60 分鍾

對於基於 ssh 的訪問,您將使用 ssh 代理,它會在需要時提供 ssh 密鑰。 這需要在您的計算機上生成密鑰,將公鑰存儲在遠程服務器上並將私鑰添加到相關的密鑰庫中。

我找到了一種在命令行上為 https 連接提供憑據的方法。 您只需要指定完整的 URL 到 git pull 並在其中包含憑據:

git pull https://username:password@mygithost.com/my/repository

您之前不需要使用憑據克隆存儲庫,這意味着您的憑據不會以.git/config結尾。 (但要確保你的 shell 不會背叛你並將命令行存儲在歷史文件中。)

沒有直接回答問題,但我在尋找一種方法時發現了這個問題,基本上,每次拉動遠程服務器時都不會重新輸入密碼

好吧, git允許您在有限的時間內緩存您的憑據。 它可以在git config中自定義,這個頁面解釋得很好:

https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux

在終端中,運行:

$ git config --global credential.helper cache
# Set git to use the credential memory cache

要自定義緩存超時,您可以執行以下操作:

$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)

然后,您的憑據將在請求的時間內存儲在內存中。

如果我們在密碼中沒有@,下面的命令將起作用: git pull https://username:pass@word@mygithost.com/my/repository如果密碼中有@,則將其替換為 %40,如下所示: git pull https://username:pass%40word@mygithost.com/my/repository

使用憑據幫助程序命令行選項

git -c credential.helper='!f() { echo "password=mysecretpassword"; }; f' fetch origin

我剛剛經歷了這個,所以在我最終使用Gitlab 訪問令牌時提供了我的答案,盡管有些人可能需要Github 訪問令牌

我更願意使用 SSH,但有一個 gitlab 錯誤阻止了它。 將我的密碼放在 .netrc 或 URL 中並不理想。 憑據管理器需要在服務器重新啟動時重新啟動,這並不理想。

因此,我選擇了一個可以像這樣使用的訪問令牌: git clone https://<username>:<accessToken>@gitlab.com/ownerName/projectName.git

訪問令牌與 url 一起存儲,因此這是不安全的,但它比使用 username:password 更安全,因為訪問令牌可以僅限於某些操作並且可以輕松撤銷。

一旦它被克隆下來(或 URL 被手動更新),你所有的 git 請求都將使用訪問令牌,因為它存儲在 URL 中。

如果您希望更新已克隆的存儲庫,這些命令可能會有所幫助:

git remote show origin

git remote remove origin

git remote add origin https://<username>:<accessToken>@gitlab.com/ownerName/projectName.git

編輯:一定要檢查下面的評論。 我添加了 2 個重要注釋。

您可以只使用用戶名而無需使用密碼,因為密碼會在 git bash 中提示。

git pull https://username@github.com/username/myrepo

要么

 git pull https://username@github.com/myrepo

在 Google 和 stackoverflow 上搜索了一段時間后,我沒有找到我的問題的答案,所以我想在這里分享我的解決方案。

git config --global credential.helper "/bin/bash /git_creds.sh"
echo '#!/bin/bash' > /git_creds.sh
echo "sleep 1" >> /git_creds.sh
echo "echo username=$SERVICE_USER" >> /git_creds.sh
echo "echo password=$SERVICE_PASS" >> /git_creds.sh

# to test it
git clone https://my-scm-provider.com/project.git

我也是為 Windows 做的。 完整答案在這里

請注意,git 憑據助手“store” 存儲未加密密碼的方式隨 Git 2.5+(2014 年第二季度)發生了變化。
請參閱Junio C Hamano ( gitster )提交 17c7f4d

credential-xdg

調整憑證助手的示例“ store ”后端,以在指定時遵循 XDG 配置文件位置。

醫生現在說:

如果未指定:

  • 憑證將從~/.git-credentials$XDG_CONFIG_HOME/git/credentials中搜索,並且
  • credentials 將被寫入~/.git-credentials如果它存在,或者$XDG_CONFIG_HOME/git/credentials如果它存在而前者不存在。

如果您希望在 Gitlab ( gitlab-ci.yml ) 上的 CI/CD 腳本中執行此操作。 你可以使用

git pull $CI_REPOSITORY_URL

這將轉化為:

git pull https://gitlab-ci-token:[MASKED]@gitlab.com/gitlab-examples/ci-debug-trace.gi

而且我很確定它使用的令牌是臨時的/每個作業令牌 - 因此這種方法的安全漏洞大大減少了。

我只是使用了以下命令並使用了git pull和 git 問我usernamepassword

第一的:

git config credential.helper store

接着:

git pull

暫無
暫無

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

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