git ssh密钥配置

1.运行命令配置邮箱

$ git config --global user.name "你的名字"

$ git config --global user.email "你的邮箱"

大多数 Git 服务器都会选择使用 SSH 公钥来进行授权。系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个。生成公钥的过程在所有操作系统上都差不多。首先你要确认一下本机是否已经有一个公钥

$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts config            id_dsa.pub

看一下有没有id_rsa和id_rsa.pub(或者是id_dsa和id_dsa.pub之类成对的文件),有 .pub 后缀的文件就是公钥,另一个文件则是密钥。

假如没有这些文件,甚至连 .ssh 目录都没有,可以用 ssh-keygen 来创建。该程序在 Linux/Mac 系统上由 SSH 包提供,而在 Windows 上则包含在 MSysGit 包里:

$ ssh-keygen -t rsa -C "[email protected]"

Creates a new ssh key using the provided email # Generating public/private rsa key pair.

Enter file in which to save the key (/home/you/.ssh/id_rsa):

添加公钥到你的远程仓库(github)

1、查看你生成的公钥:

$ cat ~/.ssh/id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0X6L1zLL4VHuvGb8aJH3ippTozmReSUzgntvk434aJ/v7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8vR3c8E7CjZN733f5AL8uEYJA+YZevY5UCvEg+umT7PHghKYaJwaCxV7sjYP7Z6V79OMCEAGDNXC26IBMdMgOluQjp6o6j2KAdtRBdCDS/QIU5THQDxJ9lBXjk1fiq9tITo/aXBvjZeD+gH/Apkh/0GbO8VQLiYYmNfqqAHHeXdltORn8N7C9lOa/UW3KM7QdXo6J0GFlBVQeTE/IGqhMS5PMln3 admin@admin-PC

2、登陆你的github帐户。点击你的头像,然后 Settings -> 左栏点击 SSH and GPG keys -> 点击 New SSH key

3、然后你复制上面的公钥内容,粘贴进“Key”文本域内。 title域,自己随便起个名字。

4、点击 Add key。

完成以后,验证下这个key是不是正常工作:

$ ssh -T [email protected]

Attempts to ssh to github

如果,看到:

Hi xxx! You've successfully authenticated, but GitHub does not # provide shell access.

恭喜你,你的设置已经成功了。

修改git的remote url

$ git remote -v
origin https://github.com/someaccount/someproject.git (fetch)
origin https://github.com/someaccount/someproject.git (push)

如果是以上的结果那么说明此项目是使用https协议进行访问的(如果地址是git开头则表示是git协议)

你可以登陆你的github,看到你的ssh协议相应的url:

git remote set-url origin [email protected]:someaccount/someproject.git

然后你可以再用命令 git remote -v 查看一下,url是否已经变成了ssh地址。

然后你就可以愉快的使用git fetch, git pull , git push,再也不用输入烦人的密码了

猜你喜欢

转载自www.cnblogs.com/ljh--/p/12745447.html