17.sourceTree使用SSL和Https来管理githu

2020-06-18  本文已影响0人  枫之叶_小乙哥

原文链接:https://blog.csdn.net/s740556472/java/article/details/80318886

1.git 远程仓库两种协议

在windows下的git客户端


20180515093857315.png

在用户的.ssh下生成了两个SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。


20180515095045687.png
$ ssh-keygen -t rsa -C "youremail@example.com"

在gitlab设置公钥秘钥


20180515095055681.png 20180515095104913.png

打开本地的id_rsa.pub(公钥),将内容复制进去即可


20180515095112497.png

github端配置完毕后,看本地的git 如何添加远程仓库,以下是重头戏:

第一步,查看当前git的远程仓库版本:

$ git remote -v

此时若什么都没有显示说明,git无远程仓库。

第二步,添加ssh协议的远程仓库:

$ git remote add origin git@github.com:unlimitbladeworks/Data-Struts-Learning.git

再次查看

$ git remote -v
origin  git@github.com:unlimitbladeworks/Data-Struts-Learning.git (fetch)
origin  git@github.com:unlimitbladeworks/Data-Struts-Learning.git (push)

当前,我就是用的这种方式连接的github,好处是每次提交代码时,不需要重复来回输入用户名和密码。

使用公司网合并前一天晚上更新到github上的代码时,报出如下错误:

$ git pull origin master
ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.

得知第一种协议被禁掉后,只能换一种连接进行合并本地仓库了。继续往下看另一种协议。

2.切换成 https协议连接github

依然是先查看当前远程仓库使用的那种协议连接:

$ git remote -v
origin  git@github.com:unlimitbladeworks/Data-Struts-Learning.git (fetch)
origin  git@github.com:unlimitbladeworks/Data-Struts-Learning.git (push)

移除掉远程仓库的配置

$ git remote rm origin

重新添加新的远程仓库,以https的形式:

git remote add origin https://github.com/unlimitbladeworks/Data-Struts-Learning.git

https的地址其实就是github上项目对应的地址,如下图:


2018051510054989.png

再次查看:

$ git remote -v
origin  https://github.com/unlimitbladeworks/Data-Struts-Learning.git (fetch)
origin  https://github.com/unlimitbladeworks/Data-Struts-Learning.git (push)

完事以上切换操作,其实问题就已经解决了。

$ git pull origin master
remote: Counting objects: 21, done.
remote: Compressing objects: 100% (8/8), done.
Unpacking objects: 100% (21/21), done.
remote: Total 21 (delta 5), reused 21 (delta 5), pack-reused 0
From https://github.com/unlimitbladeworks/Data-Struts-Learning
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

3.其他问题

从SSL切换成https后, 并不能直接使用https.
在控制台使用git clone拷贝代码的时候, 如果报如下错误

fatal: unable to access 'https://xxx/xxxx/xxxxx/xxxxx.git/':  SSL: no alternative certificate subject name matches target host name 127.0.0.1

则,解决方法如下, 需要禁用掉SSL, 才能正常使用https管理git

git config --global http.sslVerify false
上一篇 下一篇

猜你喜欢

热点阅读