首页 > 技术文章 > github配置ssh-key以及push去掉密码

woniudada 2017-08-26 02:54 原文

https://github.com/usernidaye/ssh-all

Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的git则需要SSH的配置。

github的SSH配置如下:

一 、

设置Git的user name和email:

$ git config --global user.name "xuhaiyan"
$ git config --global user.email "haiyan.xu.vip@gmail.com"

 

二、生成SSH密钥过程:
1.查看是否已经有了ssh密钥:cd ~/.ssh
如果没有密钥则不会有此文件夹,有则备份删除
2.生存密钥:

$ ssh-keygen -t rsa -C “haiyan.xu.vip@gmail.com”
按3个回车,密码为空。


Your identification has been saved in /home/tekkub/.ssh/id_rsa.
Your public key has been saved in /home/tekkub/.ssh/id_rsa.pub.
The key fingerprint is:
………………

 

最后得到了两个文件:id_rsa和id_rsa.pub


3.添加密钥到ssh:ssh-add 文件名
需要之前输入密码。
4.在github上添加ssh密钥,这要添加的是“id_rsa.pub”里面的公钥。

打开https://github.com/ ,登陆xuhaiyan825,然后添加ssh。

5.测试:ssh git@github.com

The authenticity of host ‘github.com (207.97.227.239)’ can’t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,207.97.227.239′ (RSA) to the list of known hosts.
ERROR: Hi tekkub! You’ve successfully authenticated, but GitHub does not provide shell access
Connection to github.com closed.

6. 问题:在使用git代码仓库时,使用git clone 获取代码时,如果使用的是https协议,则在每次push时需要输入账号密码。

  

1.方法一 

1.1 创建文件存储Git用户名和密码

在%HOME%目录中,一般为C:\users\Administrator,也可以是你自己创建的系统用户名目录,反正都在C:\users\中。文件名为.git-credentials,由于在Window中不允许直接创建以"."开头的文件,所以需要借助git bash进行,打开git bash客户端,进行%HOME%目录,然后用touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式:

touch .git-credentials

vim .git-credentials

https://{username}:{password}@github.com

1.2 添加Git Config 内容

进入git bash终端, 输入如下命令:

git config --global credential.helper store

执行完后查看%HOME%目录下的.gitconfig文件,会多了一项:

[credential]

    helper = store

重新开启git bash会发现git push时不用再输入用户名和密码

 

2.方法二

2.1 添加环境变量

在windows中添加一个HOME环境变量,变量名:HOME,变量值:%USERPROFILE%

%HOME% 目录指的是系统盘下的“\Documents and Settings\你的用户名

2.2 创建git用户名和密码存储文件

进入%HOME%目录,新建一个名为"_netrc"的文件,文件中内容格式如下:

machine {git account name}.github.com
login your-usernmae
password your-password

重新打开git bash即可,无需再输入用户名和密码

 

 

推荐阅读