如何避免在每次 git push 时指定用户名和密码? [英] How do I avoid the specification of the username and password at every git push?

查看:60
本文介绍了如何避免在每次 git push 时指定用户名和密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git push 我的工作到远程 Git 存储库.

每次push都会提示我输入usernamepassword.我想每次推送都避免它,但如何配置以避免它?

解决方案

1.生成 SSH 密钥

Linux/Mac

打开终端以创建 ssh 密钥:

cd ~ #你的家目录ssh-keygen -t rsa #按回车键获取所有值

对于 Windows

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

  1. 使用 Putty Gen 生成密钥
  2. 将密钥导出为开放的 SSH 密钥

这是关于上述步骤的腻子生成器的演练

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

此步骤会有所不同,具体取决于遥控器的设置方式.

  • 如果是 GitHub 存储库并且您具有管理权限,请转到设置 并单击添加 SSH 密钥".将 ~/.ssh/id_rsa.pub 的内容复制到标有密钥"的字段中.

  • 如果您的存储库由其他人管理,请将您的 id_rsa.pub 提供给管理员.

  • 如果你的远程仓库由你管理,你可以使用这个命令,例如:

    scp ~/.ssh/id_rsa.pub YOUR_USER@YOUR_IP:~/.ssh/authorized_keys/id_rsa.pub

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

的表单

如果您已完成上述步骤但仍收到密码提示,请确保您的 repo URL 为以下形式

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

相对于

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

要查看您的存储库 URL,请运行:

git remote show origin

您可以通过以下方式更改网址:

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

[1] 此部分包含来自 Eric P

I git push my work to a remote Git repository.

Every push will prompt me to input username and password. I would like to avoid it for every push, but how to configure to avoid it?

解决方案

1. Generate an SSH key

Linux/Mac

Open terminal to create ssh keys:

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

For Windows

(Only works if the commit program is capable of using certificates/private & public ssh keys)

  1. Use Putty Gen to generate a key
  2. Export the key as an open SSH key

Here is a walkthrough on putty gen for the above steps

2. Associate the SSH key with the remote repository

This step varies, depending on how your remote is set up.

  • If it is a GitHub repository and you have administrative privileges, go to settings and click 'add SSH key'. Copy the contents of your ~/.ssh/id_rsa.pub into the field labeled 'Key'.

  • If your repository is administered by somebody else, give the administrator your id_rsa.pub.

  • If your remote repository is administered by your, you can use this command for example:

    scp ~/.ssh/id_rsa.pub YOUR_USER@YOUR_IP:~/.ssh/authorized_keys/id_rsa.pub

3. Set your remote URL to a form that supports SSH 1

If you have done the steps above and are still getting the password prompt, make sure your repo URL is in the form

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

as opposed to

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

To see your repo URL, run:

git remote show origin

You can change the URL with:

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

[1] This section incorporates the answer from Eric P

这篇关于如何避免在每次 git push 时指定用户名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆