【Git】使用码云

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

使用GitHub时,国内的用户经常遇到的问题是访问速度太慢,有时候还会出现无法连接的情况(原因你懂的)。

如果我们希望体验Git飞一般的速度,可以使用国内的Git托管服务——码云(gitee.com)。

和GitHub相比,码云也提供免费的Git仓库。此外,还集成了代码质量检测、项目演示等功能。对于团队协作开发,码云还提供了项目管理、代码托管、文档管理的服务,5人以下小团队免费。

码云的免费版本也提供私有库功能,只是有5人的成员上限。

上传SSH公钥

使用码云和使用GitHub类似,我们在码云上注册账号并登录后,需要先上传自己的SSH公钥。选择右上角用户头像 -> 菜单“设置”
然后选择“SSH公钥”,填写一个便于识别的标题,然后把用户主目录下的.ssh/id_rsa.pub文件的内容粘贴进去:
在这里插入图片描述

在这里插入图片描述

点击“确定”即可完成并看到刚才添加的Key:
在这里插入图片描述
创建项目

如果我们已经有了一个本地的git仓库(例如,一个名为learngit的本地库),如何把它关联到码云的远程库上呢?

首先,我们在码云上创建一个新的项目,选择右上角用户头像旁边的“+”,选择创建仓库
在这里插入图片描述
项目名称最好与本地库保持一致:
然后,我们在本地库上使用命令git remote add把它和码云的远程库关联:
在这里插入图片描述

$ git remote add origin [email protected]:runjichi/learngit.git
fatal: remote origin already exists.

这说明本地库已经关联了一个名叫origin的远程库,此时,可以先用git remote -v查看远程库信息:

$ git remote -v
origin  https://github.com/944932343/learngit.git (fetch)
origin  https://github.com/944932343/learngit.git (push)

可以看到,本地库已经关联了origin的远程库,并且,该远程库指向GitHub。

我们可以删除已有的GitHub远程库:

$ git remote rm origin

再关联码云的远程库(注意路径中需要填写正确的用户名):

$ git remote add origin [email protected]:runjichi/learngit.git

此时,我们再查看远程库信息:

$ git remote -v
origin  [email protected]:runjichi/learngit.git (fetch)
origin  [email protected]:runjichi/learngit.git (push)

现在可以看到,origin已经被关联到码云的远程库了。通过git push命令就可以把本地库推送到Gitee上。

$ git push origin master
To gitee.com:runjichi/learngit.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:runjichi/learngit.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.

因为在码云上新建项目的时候, 勾选了"使用Readme文件初始化这个项目", 所以会生成一个如下的文件, 而你本地库并没有, 所以出错了. 提示使用 ’ git pull … ’ 命令…
git pull命令的作用是:取回远程主机某个分支的更新,再与本地的指定分支合并。

将gitee上的文件和本地库合并:

$ git pull --rebase origin master
From gitee.com:runjichi/learngit
 * branch            master     -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
Applying: wrote a readme file
......

再次推送远程

$ git push origin master
Counting objects: 35, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (33/33), done.
Writing objects: 100% (35/35), 2.79 KiB | 0 bytes/s, done.
Total 35 (delta 21), reused 0 (delta 0)
remote: Powered By Gitee.com
To gitee.com:runjichi/learngit.git
   d822249..c69706c  master -> master

现在查看下远程仓库代码
在这里插入图片描述
关联多个远程仓库

有的小伙伴又要问了,一个本地库能不能既关联GitHub,又关联码云呢?

答案是肯定的,因为git本身是分布式版本控制系统,可以同步到另外一个远程库,当然也可以同步到另外两个远程库。

使用多个远程库时,我们要注意,git给远程库起的默认名称是origin,如果有多个远程库,我们需要用不同的名称来标识不同的远程库。

仍然以learngit本地库为例,我们先删除已关联的名为origin的远程库:

$ git remote rm origin

然后,先关联GitHub的远程库:

$ git remote add github [email protected]:944932343/learngit.git

注意,远程库的名称叫github,不叫origin了。

接着,再关联码云的远程库:

$ git remote add gitee [email protected]:runjichi/learngit.git

同样注意,远程库的名称叫gitee,不叫origin

现在,我们用git remote -v查看远程库信息,可以看到两个远程库:

$ git remote -v
gitee   [email protected]:runjichi/learngit.git (fetch)
gitee   [email protected]:runjichi/learngit.git (push)
github  [email protected]:944932343/learngit.git (fetch)
github  [email protected]:944932343/learngit.git (push)

如果要推送到GitHub,使用命令:

git push github master

如果要推送到码云,使用命令:

git push gitee master

这样一来,我们的本地库就可以同时与多个远程库互相同步:

┌─────────┐ ┌─────────┐
│ GitHub  │ │  Gitee  │
└─────────┘ └─────────┘
     ▲           ▲
     └─────┬─────┘
           │
    ┌─────────────┐
    │ Local Repo  │
    └─────────────┘

小结

与远程仓库关联git remote add origin 远程仓库地址(我们已经在
【Git】添加远程库
中学过这个命令了,忘记了就去复习下)
查看远程库信息git remote -v
删除已有的远程库git remote rm origin
添加远程仓库的关联git remote add <name> <url>
推送到远程git push [remote-name] [branch-name]

猜你喜欢

转载自blog.csdn.net/u010356768/article/details/86644829