点击上方关注我们!
很多写开源项目的小伙伴都会将代码托管到Github上,但国内访问GitHub有时候会出现网络问题,随着近些年码云Gitee的火热,也有不少用户选择码云做为远程仓库。不少人一般会同时使用GitHub和Gitee,为了提高开源项目的曝光度,会选择将代码同时在两个平台进行托管。
先来查看下当前项目的远程仓库:
git remote
正常来说会输出远程仓库名:
origin
这个origin就是一个指向远程仓库的名称,是在clone时git为我们默认创建的。
我们可以通过命令查看origin指向的远程仓库地址:
git remote -v
输出结果为:
origin https://github.com/username/xxx.git (fetch)
origin https://github.com/username/xxx.git (push)
该命令会显示读写远程仓库的名称和地址,我这里指向的是Github。
为了区分后面添加Gitee后的远程仓库,建议将仓库重命名,为了好识别,这里将名称改成 github 吧。输入命令:
命令格式:git remote rename <old_remote> <new_remote>
git remote rename origin github
输入查看远程仓库命令,验证下是否成功,输出结果:
github https://github.com/username/xxx.git (fetch)
github https://github.com/username/xxx.git (push)
下面我们再添加Gitee上的远程仓库,首先在Gitee上创建一个空的仓库,名称与Github上相同。
然后在【克隆/下载】处复制地址。输出添加远程仓库命令:
命令格式:git remote add <remote><url>
git remote add gitee https://gitee.com/username/xxx.git
再来验证下是否成功,输出如下结果,说明成功:
gitee https://gitee.com/username/xxx.git (fetch)
gitee https://gitee.com/username/xxx.git (push)
github https://github.com/username/xxx.git (fetch)
github https://github.com/username/xxx.git (push)
有了多个远程仓库,推送和拉取再也不能像以前那样git push和git pull了,必须得加上远程仓库的名称,以识别操作的是哪个远程仓库。
命令格式:
git push <remote><branch>
git pull <remote><branch>
如:
git push github main
git pull github main
git push gitee main
git pull gitee main
如果不想每次操作都带着分支,需要将本地分支与远程分支进行关联:
命令格式:git branch --set-upstream-to=<remote>/<remote_branch> <local_branch>
如:
git branch --set-upstream-to=gitee/main main
关联后就可以不指定分支了:
git push github
git pull github
git push gitee
git pull gitee
如果想要移除一个远程仓库,可以使用下面命令:
命令格式:
git remote remove <remote>
或
git remote rm <remote>
如:
git remote remove gitee
执行移除远程仓库后,该仓库在本地的所有分支与配置信息也会一并删除。
当在拉取时报下面错误:
You asked to pull from the remote 'gitee', but did not specify
a branch. Becausethisisnot the default configured remote
for your current branch, you must specify a branch on the command line.
表示本地分支与远程分支未做关联,进行关联一下即可,
命令格式:git branch --set-upstream-to=<remote>/<remote_branch> <your_branch>
git branch --set-upstream-to=gitee/main main
当执行推送操作时,提示下面信息:
remote: Youdonot have permission push to this repository
fatal: unable to access 'https://gitee.com/gozhuyinglong/blog-demos.git/': The requested URL returned error: 403
表示没有远程仓库的权限,应该首先查看远程仓库是否公开,再检查本地账号和密码是否正确。
登录Gitee,检查该代码库是否公司。若未公开,则设置为公开。
如下图所示,当你每次push代码时,都会弹出下面登录框。
这个其实不算错误,只是要登录而已,不过每次登录也挺麻烦的。
我们可以将远程地址改为SSH地址,移除现在的github地址,重新添加ssh地址,添加好地址后,还需要在github上设置SSH Key。
本文参考自文章:https://blog.csdn.net/gozhuyinglong/article/details/113861993,侵删!
长按二维码
关注我们