远程仓库是指托管在网络上的项目仓库,现在互联网上有很多项目托管平台,比如码云、github、gitlab等等。当然如果你不想你的代码开源就要花费一些费用获得独立的空间,或者在自己的服务器上搭建自己的项目仓库。
在本篇文章中我以连接gitlab远程仓库为例。由于本地git仓库和gitlab仓库之间的传输是通过ssh加密的,所以必须要让gitlab仓库认证你ssh key,在此之前,必须要生成ssh key。
一、生成ssh key。
【git连接gitlab远程仓库】首先新建一个.ssh文件夹
mkdir ~/.ssh

文章图片
进入到此文件夹中,并执行生成ssh key的命令。注意将此处的邮箱地址换成自己的,然后按三次回车。这时候在.ssh文件夹下就得到了id_rsa和id_rsa.pub两个文件。其中id_rsa中的内容便是私钥,id_rsa.pub中的内容便是公钥,可以在C:\Users\用户目录\.ssh目录下找到这两个文件。
cd ~/.sshssh-keygen -t rsa -C “Anhua2015@temp.com”

文章图片
二、添加ssh key。
接下来就是在gitlab远程仓库中添加ssh key了。首先你要注册一个gitlab账号,登陆之后按如下步骤操作。
点击Settings

文章图片
找到在左侧菜单中找到ssh key,在下面图片中的2处输入上面所获得的公钥,点击Add key即可。

文章图片
三、测试连接。
为了验证是否成功,输入以下命令。
ssh -T git@github.com //如果你用的是githubssh -T git@gitlab.com //如果你用的是gitlab

文章图片
连接成功。
四、创建远程仓库并提交本地项目。
首先点击项目找到您的项目

文章图片
,点击Create a project

文章图片
填写项目名称,然后点击下面的Create project即可。

文章图片
之后的操作他会给你提示。

文章图片
至此就可以正常使用远程仓库了。