git系列文章目录
第八章 git SSH免密登录的使用前言
虽然Windows系统提供了凭据功能,但是介绍ssh提交,的免密操作一、生成密钥
进入当前用户的目录
双击打开或者命令方式
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop
$ cd ~
Apple@DESKTOP-ECLNIU2 MINGW64 ~
$ pwd
/c/Users/Apple
Apple@DESKTOP-ECLNIU2 MINGW64 ~
$
然后在当前文件夹下创建SSH 密钥
Apple@DESKTOP-ECLNIU2 MINGW64 ~
$ ssh-keygen -t rsa -C 18261897435@139.com
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Apple/.ssh/id_rsa):
Created directory '/c/Users/Apple/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Apple/.ssh/id_rsa
Your public key has been saved in /c/Users/Apple/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:QEZzuGzZzFBRmmp+DSu0o1VXEvxp+0KpKjjrWfONv6M 18261897435@139.com
The key's randomart image is:
+---[RSA 3072]----+
| .=o++o |
| ooo o.. |
| ..Bo .... |
| =o+ o+ |
| .+ S .. o |
| + o = + |
| .O o .o . |
| o=.* oo . . |
| .=o .E++o . |
+----[SHA256]-----+
Apple@DESKTOP-ECLNIU2 MINGW64 ~
$
此时创建了.ssh文件夹和两个文件
二、使用步骤
1.使用VSCODE打开.pub文件复制其中的内容
2.打开github或者gitee进入设置选项设置密钥
选择SSH公钥
标题随便起,但要方便记忆
输入刚才vscode打开的密钥复制的key到公钥
点击确定
输入密码验证后就可以使用了
3.使用密钥
给SSH地址起一个别名
复制地址
给ssh地址起别名
如下(示例):
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git remote -v
origin https://gitee.com/gitee18261897435/git-resp.git (fetch)
origin https://gitee.com/gitee18261897435/git-resp.git (push)
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git remote add origin_ssh git@gitee.com:gitee18261897435/git-resp.git
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git remote -v
origin https://gitee.com/gitee18261897435/git-resp.git (fetch)
origin https://gitee.com/gitee18261897435/git-resp.git (push)
origin_ssh git@gitee.com:gitee18261897435/git-resp.git (fetch)
origin_ssh git@gitee.com:gitee18261897435/git-resp.git (push)
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$
别名起成功了
创建一个Test100.txt用于演示登录
注意第一次使用免密登录的时候需要输入一个yes
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git add Test100.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git commit -m "项目经理添加了一个Test100.txt用于演示SSH_Key登录" Test100.txt
[master 02135f1] 项目经理添加了一个Test100.txt用于演示SSH_Key登录
1 file changed, 1 insertion(+)
create mode 100644 Test100.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git push origin_ssh master
The authenticity of host 'gitee.com (212.64.62.183)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git push origin_ssh master
The authenticity of host 'gitee.com (212.64.62.183)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
To gitee.com:gitee18261897435/git-resp.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'gitee.com:gitee18261897435/git-resp.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$
因为没有进行远程库拉取,先拉取后再提交
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git pull origin_ssh master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (4/4), 1.35 KiB | 277.00 KiB/s, done.
From gitee.com:gitee18261897435/git-resp
* branch master -> FETCH_HEAD
* [new branch] master -> origin_ssh/master
Merge made by the 'ort' strategy.
Test2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 Test2.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$ git push origin_ssh master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 581 bytes | 581.00 KiB/s, done.
Total 5 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To gitee.com:gitee18261897435/git-resp.git
7a79c73..a56a9e1 master -> master
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/GitResp (master)
$
检查远程库查看推送成功