我把我的工作推到一个远程git存储库。

每次推送都会提示我输入用户名和密码。我想避免它的每一个推送,但如何配置以避免它?


当前回答

我使用的是https链接(https://github.com/org/repo.git) 而不是SSH链接;

git@github.com:org/repo.git  

转换为我解决了这个问题!

其他回答

无限期保存

您可以使用git-credential-store

git config credential.helper store

在文件系统中保存未加密的密码:

使用此助手将在磁盘上存储未加密的密码,仅受文件系统权限的保护。如果这不是一种可以接受的安全折衷,请尝试git-凭据-缓存,或寻找与操作系统提供的安全存储集成的帮助器。

使用暂停

使用git-credential-cache,默认保存密码15分钟。

git config credential.helper cache

要设置不同的超时,使用——timeout(这里是5分钟)

git config credential.helper 'cache --timeout=300'

安全无限保存(OS X和Windows)

If you’re using a Mac, Git comes with an “osxkeychain” mode, which caches credentials in the secure keychain that’s attached to your system account. This method stores the credentials on disk, and they never expire, but they’re encrypted with the same system that stores HTTPS certificates and Safari auto-fills. Running the following on the command line will enable this feature: git config --global credential.helper osxkeychain. You'll need to store the credentials in the Keychain using the Keychain app as well. If you’re using Windows, you can install a helper called “Git Credential Manager for Windows.” This is similar to the “osxkeychain” helper described above, but uses the Windows Credential Store to control sensitive information. It can be found at https://github.com/Microsoft/Git-Credential-Manager-for-Windows. [emphases mine]

你必须设置一个SSH私钥,你可以看看这个页面,如何在Mac上进行设置,如果你在linux上,指南应该是差不多的,在Windows上你需要像MSYS这样的工具。

我在Windows上的解决方案:

右键单击目录,并选择“Git Bash Here”。 ssh-keygen -t rsa(所有值都按enter键) 您的公钥已保存在/c/Users/<your_user_name_here>/.ssh/id_rsa.pub 从指定文件复制公钥。 去你的GitHub配置文件=>设置=> SSH和GPG密钥=>新的SSH密钥=>粘贴您的SSH密钥=>保存

我建议使用凭证管理器来存储GitHub凭证。使用git config——global credential。助手存储是不安全的,因为它以明文存储GitHub密码。 对于Linux, libsecret是一个很好的替代方案。对于Ubuntu和其他一些linux发行版,您可以执行以下操作:

安装:

sudo apt-get update
sudo apt install libsecret-1-0 libsecret-1-dev
sudo apt install gnome-keyring

创建

cd /usr/share/doc/git/contrib/credential/libsecret/
Sudo make 

配置git使用libsecret存储密码

git config --global credentail.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

在此设置后输入一次密码后,git凭据将由libsecret存储。

1. 生成SSH密钥

Linux -麦克

打开终端创建ssh密钥:

cd ~                 #Your home directory
ssh-keygen -t rsa    #Press enter for all values

对于Windows

(只有当提交程序能够使用证书/私有和公共ssh密钥时才有效)

使用Putty Gen生成密钥 导出为打开的SSH密钥

下面是关于上述步骤的腻子生成的演练

2. 将SSH密钥与远程存储库关联

这一步不同,取决于遥控器的设置方式。

如果它是一个GitHub存储库,你有管理权限,进入设置并单击“添加SSH密钥”。复制~/.ssh/id_rsa的内容。pub到标记为“Key”的字段中。 如果您的存储库是由其他人管理的,请将您的id_rsa.pub交给管理员。 如果您的远程存储库由您的管理,您可以使用此命令,例如: scp ~ / . ssh / id_rsa。酒吧YOUR_USER@YOUR_IP: ~ / . ssh / authorized_keys / id_rsa . pub

3.将远程URL设置为支持SSH 1的表单

如果您已经执行了上述步骤,但仍然得到密码提示,请确保表单中有您的回购URL

git+ssh://git@github.com/username/reponame.git

而不是

https://github.com/username/reponame.git

要查看您的回购URL,运行:

git remote show origin

你可以用以下方法更改URL:

git remote set-url origin git+ssh://git@github.com/username/reponame.git

[1]本节结合了Eric P的答案