搭建多近程仓库

1、查看近程仓库

查看下以后我的项目的近程仓库

git remote

默认的话应该会输入:

origin

这个origin就是一个指向近程仓库的名称,是你在clone时 git 为你默认创立的。

能够通过命令查看origin指向的近程仓库地址:

git remote -v

输入后果:

origin  https://github.com/SoftLeaderGy/StartRedis.git (fetch)origin  https://github.com/SoftLeaderGy/StartRedis.git (push)

该命令会显示读写近程仓库的名称和地址,我这里指向的是Github。
2、近程仓库重命名
既然这个地址是Github,为了好辨认,就将名称改成 github 吧。输出命令: git remote

rename <old_remote> <new_remote>
git remote rename origin github

输出查看近程仓库命令,验证下是否胜利,输入后果:

github  https://github.com/SoftLeaderGy/StartRedis.git (fetch)github  https://github.com/SoftLeaderGy/StartRedis.git (push)

3、增加另一个近程仓库

上面咱们再增加Gitee上的近程仓库,首先在Gitee上创立一个空的仓库,名称与Github上雷同。
而后在【克隆/下载】处复制地址。


  • 输出增加近程仓库命令: git remote add <remote> <url>
git remote add gitee https://gitee.com/yang-guo-co...

再来验证下是否胜利,输入后果:

gitee   https://gitee.com/yang-guo-code/StartRedis.git (fetch)gitee   https://gitee.com/yang-guo-code/StartRedis.git (push)github  https://github.com/SoftLeaderGy/StartRedis.git (fetch)github  https://github.com/SoftLeaderGy/StartRedis.git (push)

4、多个近程仓库的推送/拉取
有了多个近程仓库,推送和拉取再也不能像以前那样git push和git pull了,必须得加上近程仓库的名称,以辨认操作的是哪个近程仓库。命令如下: git push <remote> <branch>、git pull <remote> <branch>:

git push github maingit pull github maingit push gitee maingit pull gitee main

如果不想每次操作都带着分支,须要将本地分支与近程分支进行关联: git branch --set-upstream-to=<remote>/<remote_branch> <local_branch>

git branch --set-upstream-to=gitee/main main

关联后就能够不指定分支了

git push githubgit pull githubgit push giteegit pull gitee