本章分为3部分内容:
一:创建远程仓库
- 首先进入github网站,找到创建仓库:

<img src=“https://img-blog.csdn.net/20180827112234617?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3B1bG9uZzA3NDg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70” width="70%’>
二:将本地仓库关联到远程仓库
- 创建SSH key
远程仓库创建完之后,是不是想开始关联本地仓库了?我们需要明白github和本地仓库之间的传输是SSH加密的,所以必须要设置SSH.我们得了解下SSH 登录,首先它需要两个密码,一个公有秘钥,一个私有秘钥,公有秘钥需要上传到公钥服务器,在这里就是github上,而私钥将会在你本机。生成SSH公私钥方式:
$ ssh-keygen -t rsa -C "626dsa47@qq.com" //注意这里的邮箱写成自己的
之后待会打印出:
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 换成自己新建的远程库
可能出现错误:fatal: 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 'git@github.com: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文件
下一篇:史上最简单的git教程|第八篇:创建和合并分支
如果有小伙伴觉得我写的不错的话可以关注一下我的博客,我会一直持续更新,也可以支持一下我的公众号哦:java架构师小密圈,会分享架构师所必须深入研究的技术,比如netty,分布式,性能优化,spring源码分析,mybatis源码分析,等等等,同时还会分享一些赚钱理财的小套路哦,欢迎大家来支持,一起学习成长,程序员不仅仅是搬瓦工!
公众号:分享系列好文章

交流群:群友互相分享资料



被折叠的 条评论
为什么被折叠?



