Git-SSH 配置和使用

来源:互联网 发布:山东软件开发 编辑:程序博客网 时间:2024/05/16 10:21

Git-SSH 配置和使用

1.设置Git的user name和email

    $ git config --global user.name "dc"    $ git config --global user.email "258263***3@qq.com"

2.生成密钥

    $ ssh-keygen -t rsa -C "258263***3@qq.com"

连续3个回车。如果不需要密码的话。
最后得到了两个文件:id_rsa和id_rsa.pub

    Your public key has been saved in ****.pub.    The key fingerprint is:    SHA256:HxL1BS2*Pbng1O+cGnTfxaA7zP63OGuoeKbGSNjE 258263***3@qq.com    The key's randomart image is:

3.添加密钥到ssh-agent

确保 ssh-agent 是可用的。ssh-agent是一种控制用来保存公钥身份验证所使用的私钥的程序,其实ssh-agent就是一个密钥管理器,运行ssh-agent以后,使用ssh-add将私钥交给ssh-agent保管,其他程序需要身份验证的时候可以将验证申请交给ssh-agent来完成整个认证过程。

    $  eval "$(ssh-agent -s)"    Agent pid *0**

添加生成的 SSH key 到 ssh-agent

    $ ssh-add ~/.ssh/id_rsa    Enter passphrase for /c/Users/onion/.ssh/id_rsa:    Bad passphrase, try again for /c/Users/onion/.ssh/id_rsa:    Bad passphrase, try again for /c/Users/onion/.ssh/id_rsa:    Bad passphrase, try again for /c/Users/onion/.ssh/id_rsa:    Identity added: /c/Users/onion/.ssh/id_rsa (/c/Users/onion/.ssh/id_rsa)

SSHkey就保存在路径/c/Users/onion/.ssh/id_rsa下,用notepad++打开复制到GitHub即可

4.测试

    $ ssh -T git@github.com    Warning: Permanently added the RSA host key for IP address '192.**.255.112' to the list of known hosts.    Hi oniondai! You've successfully authenticated, but GitHub does not provide shell access.

出现hi说明就没问题

5.远程库配置

GitHub允许你添加多个Key。假定你有若干电脑,你一会儿在学校提交,一会儿在家里提交,只要把每台电脑的Key都添加到GitHub,就可以在每台电脑上往GitHub推送了。

    $ git remote add origin oniondai/oniondai.github.io 

请千万注意,把上面的 oniondai替换成你自己的GitHub账户名,否则,你在本地关联的就是我的远程库,关联没有问题,但是你以后推送是推不上去的,因为你的SSH Key公钥不在我的账户列表中
接下来就可以把本地库的所有内容推送到远程库上

    $ git push -u origin master 

好啦,静静等待git把你的代码Push上去吧~是不是很简单?

参考

1.https://segmentfault.com/a/11900000026456232.http://cuiqingcai.com/423.html