我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
当前回答
有一种简单、老式的方法可以将用户凭据存储在HTTPS URL中:
https://user:password@github.com/...
您可以使用git remote set URL<remote repo><URL>更改URL
这种方法的明显缺点是必须以纯文本存储密码。您仍然可以只输入用户名(https://user@github.com/…),这至少可以为您省去一半的麻烦。
您可能更喜欢切换到SSH或使用GitHub客户端软件。
其他回答
使用凭据存储。
对于OS X和Linux上的Git 2.11+,请使用Git内置的凭据存储:
git config --global credential.helper libsecret
对于Windows上的msysgit 1.7.9+:
git config --global credential.helper wincred
对于OS X上的Git 1.7.9+,请使用:
git config --global credential.helper osxkeychain
为了安全起见,最好使用凭据,但您可以使用缓存将其保存一段时间:
git config --global credential.helper cache
git config credential.helper 'cache --timeout=3600'
您的凭据将保存3600秒。
通常你有一个远程URL,像这样,
git remote -v
origin https://gitlab.com/username/Repo.git (fetch)
origin https://gitlab.com/username/Repo.git (push)
如果您想在使用git push时跳过用户名和密码,请尝试以下操作:
git remote set-url origin https://username:password@gitlab.com/username/Repo.git
我刚刚向origin添加了相同的URL(包括密码在内的用户详细信息)。
注意:如果用户名是电子邮件Id,则不起作用。
git remote -v
origin https://username:password@gitlab.com/username/Repo.git (fetch)
origin https://username:password@gitlab.com/username/Repo.git (push)
你可以使用
git config credential.helper store
当您下次使用pull或push输入密码时,它将以明文形式存储在文件.git凭据中(有点不安全,但只需将其放入受保护的文件夹中)。
如本页所述:
git凭证存储
您还可以让Git使用Git凭据存储永久存储您的凭据,如下所示:
git config credential.helper store
注意:虽然这很方便,但Git会在项目目录下的本地文件(.git凭据)(参见下面的“home”目录)。如果不喜欢,请删除此文件并切换到使用缓存选项。
如果您希望Git恢复到每次需要时都向您请求凭据连接到远程存储库,可以运行以下命令:
git config --unset credential.helper
要将密码存储在%HOME%目录(而不是项目目录)中的.git凭据中,请使用--global标志
git config --global credential.helper store