本地Git仓库和github仓库之间的传输是通过SSH加密传输的
1. 生成密钥
ssh-keygen -t rsa -b 4096 -C "youremail@example.com"
宿主目录 /用户/username/.ssh/目录下,会生产 id_rsa.pub(公钥) 和id_rsa(密钥) 两个文件
2. ssh代理: ssh-agent
ssh-agent
是一个密钥管理器。运行ssh-agent以后,使用ssh-add
将私钥交给ssh-agent
保管,其他程序需要身份验证的时候可以将验证申请交给ssh-agent
来完成整个认证过程。
# eval `ssh-agent -s` :启动代理,直接启动一个 ssh-agent 进程eval `ssh-agent -s` #这里是反引号ssh-add –l
3. 在远程仓库李配置公钥
登录github,打开”settings”中的SSH Keys页面,然后点击“Add SSH Key”,填上任意title,在Key文本框里黏贴id_rsa.pub文件的内容。
4. 进行同步的时候可能遇到的问题
- 问题1.fatal: HTTP request failed , 403 Forbidden!
如: error: The requested URL returned error: 403 Forbidden while accessing
可以看见可能是权限问题导致
解决方法:
可以修改.git/config文件追加用户名和密码:1)编辑.git/config文件2)在[remote “origin”]下找到找到url变量3)修改url = https://github.com/user/test.git,修改为url = ssh://git@github.com/user/test.git,修改完了保存
- 问题2: 测试连接远程仓库报错
输入命令:进行测试
ssh git@github.com
可能报错:
The authenticity of host 'github.com (192.30.252.131)' 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 #这里写 yesWarning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known hosts.Permission denied (publickey).
说是主机密钥验证失败, 发现是缺少了 known_hosts 文件, 而且必须生成 github.com 的ip执行内容.
- 问题3: push到github时,每次都要输入用户名和密码的问题
原因:可能使用的https 方法,而非 shell方式
解决方式:查看远程仓库信息
git remote -vorigin https://github.com/yuquan0821/demo.git (fetch)origin https://github.com/yuquan0821/demo.git (push)
把https换成shell方式
git remote rm origin git remote add origin git@github.com:yuquan0821/demo.gitgit push origin``- 问题4: 推送时报邮箱错误错误信息:remote: error: GH007: Your push would publish a private email address.解决方法:在GitHub的你账号网页上右上角,个人的登录退出的位置,找到setting: setting->emails->Keep my email address private,把这一项去掉勾选即可。