bash 如何为git pull输入带有密码的命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11506124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enter command with password for git pull?
提问by Anton Medvedev
I want to do this command in one line:
我想在一行中执行此命令:
git pull && [my passphrase]
How to do it?
怎么做?
回答by eis
This is not exactly what you asked for, but for http(s):
这不完全是您要求的,而是针对 http(s) 的:
- you can put the password in .netrc file (_netrc on windows). From there it would be picked up automatically. It would go to your home folder with 600 permissions.
- you could also just clone the repo with
https://user:pass@domain/repo
but that's not really recommended as it would show your user/pass in a lot of places... - a new option is to use the credential helper. Note that credentials would be stored in clear text in your local config using standard credential helper. credential-helper with wincred can be also used on windows.
- 您可以将密码放在 .netrc 文件(Windows 上的 _netrc)中。从那里它会被自动拾取。它将以 600 权限转到您的主文件夹。
- 你也可以只克隆 repo ,
https://user:pass@domain/repo
但这并不是真正推荐的,因为它会在很多地方显示你的用户/通行证...... - 一个新选项是使用凭证助手。请注意,凭据将使用标准凭据帮助程序以明文形式存储在本地配置中。带有 wincred 的凭证助手也可以在 Windows 上使用。
Usage examples for credential helper
凭证助手的使用示例
git config credential.helper store
- stores the credentials indefinitely.git config credential.helper 'cache --timeout=3600'
- stores for 60 minutes
git config credential.helper store
- 无限期地存储凭据。git config credential.helper 'cache --timeout=3600'
- 储存 60 分钟
For ssh-based access, you'd use ssh agent that will provide the ssh key when needed. This would require generating keys on your computer, storing the public key on the remote server and adding the private key to relevant keystore.
对于基于 ssh 的访问,您将使用 ssh 代理,该代理将在需要时提供 ssh 密钥。这需要在您的计算机上生成密钥,将公钥存储在远程服务器上并将私钥添加到相关密钥库。
回答by holgero
I found one way to supply credentials for a https connection on the command line. You just need to specify the complete URL to git pull and include the credentials there:
我找到了一种在命令行上为 https 连接提供凭据的方法。您只需要指定 git pull 的完整 URL 并在其中包含凭据:
git pull https://username:[email protected]/my/repository
You do not need to have the repository cloned with the credentials before, this means your credentials don't end up in .git/config
. (But make sure your shell doesn't betray you and stores the command line in a history file.)
您之前不需要使用凭据克隆存储库,这意味着您的凭据不会以.git/config
. (但请确保您的 shell 不会背叛您并将命令行存储在历史文件中。)
回答by Jivan
Doesn't answer the question directly, but I found this question when searching for a way to, basically, not re-enter the password every single time I pull on a remote server.
不直接回答问题,但我在寻找一种方法时发现了这个问题,基本上,每次我拉远程服务器时都不重新输入密码。
Well, git
allows you to cache your credentials for a finite amount of time. It's customizable in git config
and this page explains it very well:
好吧,git
允许您在有限的时间内缓存您的凭据。它是可定制的git config
,这个页面很好地解释了它:
https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux
https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux
In a terminal, run:
在终端中,运行:
$ git config --global credential.helper cache
# Set git to use the credential memory cache
To customize the cache timeout, you can do:
要自定义缓存超时,您可以执行以下操作:
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
Your credentials will then be stored in-memory for the requested amount of time.
然后,您的凭据将在请求的时间内存储在内存中。
回答by VonC
Note that the way the git credential helper "store" will store the unencrypted passwordschanges with Git 2.5+ (Q2 2014).
See commit 17c7f4dby Junio C Hamano (gitster
)
请注意,git 凭证助手“存储”将存储未加密密码的方式随 Git 2.5+(2014 年第二季度)发生变化。
见提交17c7f4d通过JUNIOÇ滨野(gitster
)
credential-xdg
Tweak the sample "
store
" backend of the credential helper to honor XDG configuration file locations when specified.
credential-xdg
调整
store
凭据助手的示例“ ”后端以在指定时遵守 XDG 配置文件位置。
The doc now say:
医生现在说:
If not specified:
- credentials will be searched for from
~/.git-credentials
and$XDG_CONFIG_HOME/git/credentials
, and- credentials will be written to
~/.git-credentials
if it exists, or$XDG_CONFIG_HOME/git/credentials
if it exists and the former does not.
如果未指定:
- 将从
~/.git-credentials
和 中搜索凭据$XDG_CONFIG_HOME/git/credentials
,并且~/.git-credentials
如果存在,或者$XDG_CONFIG_HOME/git/credentials
如果存在而前者不存在,则将写入凭据。