1,清除全局
git config --global --unset user.name
git config --global --unset user.email
2,生产密钥对
ssh-keygen -t rsa -C "yourmail@gmail.com"
正常情况一路回车到最后,但是如果要生成两个的话,第二次会把第一次的覆盖。
所以在提示“Enter file in witch to save the key”的时候,输入你要生成key的名字就好了。比如两次命令生成的两个文件分别是a和b。
3,添加私钥
$ ssh-add ~/.ssh/id_rsa
如果执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:
$ ssh-agent bash
然后再运行ssh-add命令。
# 可以通过 ssh-add -l 来确私钥列表 $ ssh-add -l
# 可以通过 ssh-add -D 来清空私钥列表 $ ssh-add -D
4,修改配置文件
在 ~/.ssh 目录下新建一个config文件
touch config
添加内容:
# gitlabHost gitlab.com HostName gitlab.com User publickey IdentityFile ~/.ssh/id_rsa# githubHost github.com HostName github.com User publickey IdentityFile ~/.ssh/github_rsa
5,测试链接
ssh -T git@github.com 检查链接,成功后 服务器密钥变绿色
6,链接变更
git remote rm origin
git remote add origin github:xx
7. 配置本地git用户名和邮箱(非必须项)
如果首次push repo没有配置git的账号和邮箱,可以如下配置
1 2 3 4 5 6 7 8 9 10 11 | #全局配置 git config --global user.name "Your name" git config --global user.email your_email@gmail.com ##非必须项 #局部配置,如果没有局部配置,默认用全局配置否则优先使用局部配置 cd ~ /workspace/github_two/ git init git config user.name "Your name" git config user.email your_email@gmail.com |
8,修改rsa密码
- 打开git bash
- 输入
cd ~/.ssh
ls
确定有 id_rsa 和 id_rsa.pub文件ssh-keygen -p -f id_rsa
- 第一次输入旧密码
- 新密码
- 确认新密码