史上最简单的git教程|第七篇:远程仓库关联本地、克隆仓库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pulong0748/article/details/82110709

     本章分为3部分内容:

一:创建远程仓库

  1. 首先进入github网站,找到创建仓库:

二:将本地仓库关联到远程仓库

  1. 创建SSH key
    远程仓库创建完之后,是不是想开始关联本地仓库了?我们需要明白github和本地仓库之间的传输是SSH加密的,所以必须要设置SSH.我们得了解下SSH 登录,首先它需要两个密码,一个公有秘钥,一个私有秘钥,公有秘钥需要上传到公钥服务器,在这里就是github上,而私钥将会在你本机。生成SSH公私钥方式:
$ ssh-keygen -t rsa -C "[email protected]"    //注意这里的邮箱写成自己的 

     之后待会打印出:

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/wanghao/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in   /c/Users/wanghao/.ssh/id_rsa. //私钥位置
Your public key has been saved in   /c/Users/wanghao/.ssh/id_rsa.pub. //公钥位置
The key fingerprint is:

     要明白私钥不能泄密哦。接着我们要把公钥添加到公钥服务器也就是gitHub上,步骤如下:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

     添加成功后:
这里写图片描述
     所以可以知道,假如有多个本地库要和远程库相连,就需要配置多个SSH key,这也为分布式版本控制提供了安全保障。
2. 接下来我们要关联远程仓库了
一定要在自己的本地仓库里面运行命令:

git remote add origin git@github.com:gxx628/touchGit-remote.git  //gxx628换成自己的github用户名
//touchGit-remote.git  换成自己新建的远程库

     可能出现错误:f**atal: remote origin already exists.** 解决方式:

 1、先输入$ git remote rm origin
 2、再输入$ git remote add origin git@github.com:gxx628/touchGit-remote.git  就不会报错了!

3:把本地库内容推到远程库中:

$ git push -u origin master 

     然而又报错了,原因是之前创建远程库添加了README文件,而本地库没有,本地推到远程会有冲突:

To github.com:gxx628/touchGit-remote.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:gxx628/touchGit-remote.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.

     解决方式如下:

git pull --rebase origin master   //先运行此命令

这里写图片描述
     然后再运行此命令成功解决:

git push -u origin master

这里写图片描述
     于是我们打开github创建的仓库看一下,我们推上去的内容显示出来了:
这里写图片描述


从远程库克隆版本库

克隆版本库很简单:
这里写图片描述
     然后输入命令:

git clone https://github.com/gxx628/touchGit-remote.git

     所以如果多个人一起协作开发,只要他们从远程库克隆,然后把自己修改的内容push到远程库即可,假如它没有加入你的SSH 公钥,那你是不可以push的,由此来控制push权限。


上一篇:史上最简单的git教程|第六篇:谨慎删除git文件

猜你喜欢

转载自blog.csdn.net/pulong0748/article/details/82110709