我刚刚开始使用git,我不能让它记住我的密码,我使用cmd.exe提升,我的git主机是github,我已经创建了一个ssh密钥,就像github上的指南

但我还是得到了

*\subnus.mvc>git push origin master
Enter passphrase for key '/c/Users/Subnus/.ssh/id_rsa':

当前回答

在问题发布5年8个月零6天后再做一个解决方案也不是什么坏主意。

注意:假设您使用的是windows计算机。

下载git-credential-winstore。 运行它!如果PATH环境变量中有GIT,它应该可以工作。如果没有,运行git-credential-winstore -i C:\Path\To\Git.exe。

下次尝试提交到存储库时,系统将提示您输入凭据。应该是这样。在您更改密码之前,您将不再被要求提供您的凭证。


只是为了让你知道…您的凭据存储在Windows凭据存储中

你把我的证件放在哪里? 这个应用程序只是使用现有的Windows凭据商店来保存您的凭据。您可以通过进入控制面板>用户帐户>凭证管理器并选择“Windows凭证”来查看存储的凭证。以“git:”开头的条目来自git-credential-winstore。

其他回答

如果您为密钥文件设置了密码,则在连接时始终需要输入该密码。如果你创建了一个无密码密钥,那么你就不必每次都输入它,但是,任何访问你的密钥文件的人现在都可以连接到你的github帐户。

Ssh-agent也可以工作。试着运行它,看看它是否会记住你的密码。

我意识到这个问题已经拖延了好几年,但我无意中发现了这个问题,试图找到一个解决方案,我发现了一些适合所有专业水平的东西,所以我想我应该分享一下。

GitHub提供了一个非常有用的安装程序,使一切都很好很简单:https://help.github.com/articles/caching-your-github-password-in-git/

你可以在你的用户的主目录下创建一个。bashrc文件,比如C:/Users/youruser,然后放在那里:

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
    . "$env" >| /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

该脚本在每次bash运行后执行。因此,当git-bash启动时,您只需要输入一次密码!

某些版本的bash需要.bash_profile文件而不是.bashrc,所以以防万一克隆。bashrc:

copy .bashrc .bash_profile

让我们假设您想使用纯Git Bash解决方案,而不使用TortoiseGit或PuTTY。此外,您也不希望永久存储密码短语,因为它几乎与您在没有密码短语的情况下生成SSH密钥相同。但是您仍然需要使用一些缓存。

出于缓存目的,使用了ssh-agent进程,它包含在Git Bash发行版中。默认情况下,此进程没有启动,因此需要首先启动它。对于要缓存的任何SSH密钥,应该使用SSH -add命令将它们添加到这个进程中,该命令将提示您输入密钥的密码短语并将其存储在内存中。

其他解决方案的缺点:

Auto-launching ssh-agent like in GitHub's article asks for a passphrase right from the start when you launch Git Bash, regardless of whether you'll need to use your SSH key this session or not. If you're working with your local repo today you'll probably want to provide a passphrase only when really needed (e.g. when interacting with a remote repo). If you launch your ssh-agent like in GitLab's article with eval $(ssh-agent -s) you're probably tired of typing that in each time. Chances are, eventually, you've added those two lines to your .bashrc config to auto-launch. Downsides are the same as above plus an extra one: each time you launch a new Git Bash terminal you'll get an extra ssh-agent process (GitHub's bash script checks if that process has already started). Like the two above but especially so when you have separate SSH keys for different hosts, e.g. one for GitHub and another one for GitLab, so providing them all at once is annoying and inconvenient.

因此,这个解决方案是为那些想知道如何让Git Bash在每个Windows会话中只请求一次密码短语并且只在真正需要时才请求密码短语的人准备的。它类似于GnuPG使用default-cache-ttl提交自动签名的密码短语管理行为。

配置SSH,在需要时只使用Git Bash请求一次口令

First, we want to auto-launch the ssh-agent when starting a Git Bash shell. We'll use a modified GitHub's script for that as it checks whether the process has already started, but we don't want it to ssh-add keys right away. This script goes to your ~/.bashrc or ~/.profile or ~/.bash_profile (~ is your User's home directory like C:\Users\Username – run cd ~ and then pwd for the Git Bash to print it out): ### Start ssh-agent env=~/.ssh/agent.env agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; } agent_start () { (umask 077; ssh-agent >| "$env") # use -t here for timeout . "$env" >| /dev/null ; } agent_load_env # agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?) if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then agent_start fi unset env Now edit or create a ~/.ssh/config file and add an AddKeysToAgent option for each host stanza you want caching to be turned on (you can also turn it on globally by placing the directive at the beginning of the file before all the host declarations): # GitHub.com Host github.com Preferredauthentications publickey IdentityFile ~/.ssh/id_ed25519_github AddKeysToAgent yes # GitLab.com Host gitlab.com Preferredauthentications publickey IdentityFile ~/.ssh/id_ed25519_gitlab AddKeysToAgent yes From ssh config man page: If this option is set to yes and a key is loaded from a file, the key and its passphrase are added to the agent with the default lifetime, as if by ssh-add(1).

默认的最大生存期是永远,或者直到ssh-agent进程被杀死(可以从任务管理器手动杀死,也可以在PC关闭时杀死)。如果希望使用有限超时,可以使用ssh-agent的-t参数设置它。从上面的第一步更改bash脚本中的行,例如,键缓存生存时间为30分钟:

(umask 077; ssh-agent -t 30m >| "$env")

有关其他时间格式限定符,请参见这里。

确保你的~/。Ssh /config不包含

UseKeychain是的

防止ssh-add持久化到ssh-agent。