github配置公钥以及文件的上传

前言

      在github上传文件有两种方式,第一种是在github网站在线上传,但我在使用这种方式上传过程中,不知道说明原因总是上传不成功。所以就采用了第二种方式上传,第二种方式需要我们本地有私钥然后在github中配置公钥,然后通过git客户端工具将本地文件上传到github中,在这里我将整个配置和上传的流程记录以下,供大家参考。

环境准备:

本地安装了git客户端,下载地址https://git-scm.com/downloads

配置私钥和公钥

生成私钥和公钥

因为我的电脑已经存在了一个项目组git仓库的私钥,为了避免冲突,这里我将github的私钥命名为id_rsa_github。(同理,如果我们有多个不同的git服务器的需求时,用这种方法可以生成多个私钥和公钥来避免冲突)打开Git Bash Here命令窗口,输入命令:

ssh-keygen -t rsa -C 你的用户名

这里写图片描述
C:\Users\用户名.ssh文件夹我们会发现,公钥和私钥已经生成
这里写图片描述

配置github公钥

进入我们的github网站,点击头像右边的三角—–选择Settings,然后如下图进行操作
这里写图片描述

这里写图片描述

检测是否配置成功

输入命令检测是否配置成功:ssh -T [email protected]
如果出现以下提示说明配置成功:
Hi pengleiLiu! You've successfully authenticated, but GitHub does not provide shell access.
然而我在配置的时候却没有这么幸运,出现以下错误:
1.报错[email protected]: Permission denied (publickey).

$ ssh -T [email protected]
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of know        n hosts.
[email protected]: Permission denied (publickey).

这里写图片描述
原因:我们在生成 ssh 密钥的时候设定的私钥名字是id_rsa_github,而ssh内设的名字是id_rsa,虽然我的本地有一个id_rsa,但是这个是我git服务器的私钥,所以链接github的时候会报错。
解决:将自己设定的名字加入到ssh中

#ssh-add ~/.ssh/你的名字
ssh-add ~/.ssh/id_rsa_github

报错2:Could not open a connection to your authentication agent.
解决方法:

#开启ssh-agent
$ eval ssh-agent -s
#继续添加到ssh中
ssh-add ~/.ssh/id_rsa_github
#成功
Identity added: /c/Users/lpl/.ssh/id_rsa_github (/c/Users/lpl/.ssh/id_rsa_github )
#再次检测链接
$ ssh -T git@github.com
Hi pengleiLiu! You've successfully authenticated, but GitHub does not provide shell access.

这里写图片描述

文件上传到gitHub中

前提

你在gitHub已经存在创建好的仓库
这里写图片描述

克隆项目仓库到本地

1.在电脑本地创建一个文件夹,作为本地github的仓库
2.将github上的一个仓库进行克隆,这个仓库是我们将要上传文件的目标仓库哦
复制github仓库的路径
这里写图片描述
执行命令,然后本地仓库中多了一.个git

#克隆到本地仓库中
git clone https://github.com/pengleiLiu/config-repo-demo.git

这里写图片描述

文件上传

执行以下命令进行文件上传到github中

#将所有文件添加到仓库中,注意后边有一个.
git add .
#双引号中的文字为注释
git commit -m "配置文件"
#仓库push到github上面,此步骤需要你输入帐号和密码
$ git push -u origin master
#提交成功显示
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 324 bytes | 324.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/pengleiLiu/config-repo-demo.git
   5dfaa8a..efde8c9  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

这里写图片描述
文件上传成功!

参考资料:

https://www.cnblogs.com/popfisher/p/5731232.html
http://blog.csdn.net/samxx8/article/details/51497004
https://stackoverflow.com/questions/24154816/git-bash-could-not-open-a-connection-to-your-authentication-agent
https://www.cnblogs.com/cxk1995/p/5800196.html

猜你喜欢

转载自blog.csdn.net/plei_yue/article/details/78959525
今日推荐