缘由:使用https的方式 ,每次又要输入密码,很繁琐,就想着使用SSH的方式了,减少输入密码的时间。
由于我是使用了一段时间HTTPs方式后, 才转变成SSH的所以配置过程中采了不少坑。
官方给出的,个人感觉不是太好,太啰嗦,又抓不住重点,适合一开始就这样做
主要又三大步:
第一步是获得SSH值
如果是第一次,需要配置邮箱和密码
$ git config --global user.name "humingx"
$ git config --global user.email "humingx@yeah.net"
我直接用下列命令获取
ssh-keygen -t rsa -b 4096 -C "邮箱地址@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/a1104/.ssh/id_rsa):
Created directory ‘/c/Users/a1104/.ssh’.
Enter passphrase (empty for no passphrase): //密码直接设置为空,回车
Enter same passphrase again:
Your identification has been saved in /c/Users/a1104/.ssh/id_rsa.
Your public key has been saved in /c/Users/a1104/.ssh/id_rsa.pub.
The key fingerprint is:
ls -al ~/.ssh
//查看是否获得SSH,
total 25
drwxr-xr-x 1 a1104 *** 0 1月 21 20:29 ./
drwxr-xr-x 1 a1104 ** 0 1月 21 20:28 …/
-rw-r–r-- 1 a1104 ** 1月 21 20:29 id_rsa
-rw-r–r-- 1 a1104 ** 743 1月 21 20:29 id_rsa.pub
第二步是启动本地的SSH值
start the ssh-agent in the background
//启动
此时出现错误
/usr/bin/start: line 8: cmd: command not found
错误的原因,见这个链接:error
ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-jW**; export SSH_AUTH_SOCK;
SSH_AGENT_PID=150**; export SSH_AGENT_PID;
echo Agent pid 150**;
ssh-add ~/.ssh/id_rsa
提示错误: Could not open a connection to your authentication agent.
运行 eval $(ssh-agent)
Agent pid 13**
ssh-add ~/.ssh/id_rsa
出现下面提示:
Identity added: /c/Users/a1104/.ssh/id_rsa (/c/Users/a1104/.ssh/id_rsa)
下面是尝试运行了以下语句(这一部分,我没有理解,只是按着别人给的操作方法做的):
$ start the ssh-agent in the background’
/usr/bin/start: line 8: cmd: command not found
$ echo off
off
$ eval
ssh-agent -s`Agent pid 1507**
`
ssh-add
Identity added: /c/Users/a1104/.ssh/id_rsa (/c/Users/a1104/.ssh/id_rsa)
$ start the ssh-agent in the background
/usr/bin/start: line 8: cmd: command not found
$ clip < ~/.ssh/id_rsa.pub
bash: clip: command not found
第三步配置
出现错误提示:
Could not open a connection to your authentication agent.
解决方案Your shell is meant to evaluate that shell code output by ssh-agent. Run this instead:
eval "$(ssh-agent)"
Or if you’ve started ssh-agent already, copy paste
it to your shell prompt (assuming you’re running a Bourne-like shell).ssh commands need to know how to talk to the ssh-agent, they know that
from the SSH_AUTH_SOCK environment variable.
cat ~/.ssh/id_rsa.pub
检测:ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '******' to the list of known hosts.
Hi ***li! You've successfully authenticated, but GitHub does not provide shell access.
出现了 successfully 后当你运行 git push
时还是提示
Username for ‘https://github.com’:
是不是很崩溃?这是因为这时git还时按照之前设定的HTTPS的模式在运行,所以要更换HTTPs模式为ssh模式。
是不是很崩溃?这是因为这时git还时按照之前设定的HTTPS的模式在运行,所以要更换HTTPs模式为ssh模式。
最后放大招:(最后一步)
更换Https模式到SSH模式-教程
git remote remove origin
// 移去当前的HTTPS模式下的origin
git remote add origin git@github.com:zengquanli/Android_WebCon_text.git
//加入 SSH 模式
使用上文中的操作出现错误:
git branch --set-upstream-to=origin/master master
error: the requested upstream branch ‘origin/master’ does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run “git fetch” to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: “git push -u” to set the upstream config as you push.
根据提示信息:
git push -u origin master
//这儿的origin好像是本地分支,master是远程分支
//提示:
Warning: Permanently added the RSA host key for IP address '***' to the list of known hosts.
Everything up-to-date
Branch 'master' set up to track remote branch 'master' from 'origin'.
测试一下:
git add .
> warning: LF will be replaced by CRLF in .idea/vcs.xml.
>The file will have its original line endings in your working directory.
git commit -m "text ssh"
>[master 562d1f5] text ssh
> 2 files changed, 7 insertions(+)
>create mode 100644 .idea/vcs.xml
git push
> Enumerating objects: 18, done.
>Counting objects: 100% (18/18), done.
>Delta compression using up to 4 threads.
//成功,你需要密码了。
为其他fork的项目配置SSH
$ ls -al ~/.ssh
drwxr-xr-x 1 a1104 19760***
$ start the ssh-agent in the background
/usr/bin/start: line 8: cmd: command not found
$ ssh-agent
SSH_AUTH_SOC***
SSH_AGENT_PID=22***; export SSH_AGENT_PID;
echo Agent**;
$ git remote remove origin
$ git remote add origin git@github.com:zengquanli/ud851-Exercises.git
$ git branch --set-upstream-to=origin/master master
fatal: branch 'master' does not exist
$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'git@github.com:zengquanli/ud851-Exe rcises.git'
$ git push -u origin student
Everything up-to-date
Branch 'student' set up to track remote branch 'student' from 'origin'.