likes
comments
collection
share

Git代码仓库配置密钥SSH keys

作者站长头像
站长
· 阅读数 5

当你在Git代码仓库,比如Github或者国内的码云Gitee,创建了一个仓库后,关联本地文件,可能出现以下情况:

  • 仓库初始化
$ git commit -m "init page"
[master (root-commit) d832761] init page
 1 file changed, 7 insertions(+)
 create mode 100644 index.html
  • 关联本地文件
$ git remote add origin git@github.com:xxxxx/xxxxx.github.io.git
  • push remote仓库
$ git push -u origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
  • 失败,被告知没有访问权限,这是因为本机没有给代码仓库配置密匙
  • 开始放大招,执行命令ssh-keygen -t rsa -C 你的邮箱地址,获取public 密匙
$ ssh-keygen -t rsa -C 你的邮箱地址
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
2c:03:89:09:5c:4c:2a:b9:2b:bd:ec:2b:a0:ce:ff:22 xxxxxxxxxx@qq.com
The key's randomart image is:
+--[ RSA 2048]----+
|o +o             |
| +.+ .           |
|o.o o            |
|..   . .         |
|.     o S        |
|.o     o         |
|= .              |
|=E o             |
|.=Boo.           |
+-----------------+
  • 根据路径找到id_rsa.pub文件,复制里面的内容,这里演示粘贴到Github的personal settings的SSH keys中

Git代码仓库配置密钥SSH keys

  • 再一次push提交代码,成功,well done
$ git push -u origin master
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa':
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 293 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:xxxxx/xxxxx.github.io.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.