如何将代码同时提交到Github和码云Gitee上

微信搜索:码农StayUp
主页:https://gozhuyinglong.github.io
Gitee:https://gitee.com/gozhuyinglong/blog-demos
Github:https://github.com/gozhuyinglong/blog-demosgit

相信不少写开源项目的小伙伴都会将代码托管到Github上,但随着近些年码云Gitee的火热,也有很多用户选择码云作为远程仓库。为了提升开源项目的曝光度,会选择将代码同时在两个平台进行托管。github

那么如何将代码同时提交到Github和Gitee上呢?本文将进行详细介绍,并列出常见错误及解决方案。shell

本文目录:windows

1. 多个远程仓库的使用

多个远程仓库在项目中不多使用,但Git自己是支持的。安全

那让咱们跟着一个案例来温习一下Git命令吧:案例代码已经在Github中托管了,如今要增长Gitee远程仓库。微信

1.1 查看远程仓库

先来查看下当前项目的远程仓库ssh

git remote

不出意外,应该会输出:fetch

origin

这个origin就是一个指向远程仓库的名称,是你在clonegit 为你默认建立的。网站

能够经过命令查看origin指向的远程仓库地址:this

git remote -v

输出结果:

origin  https://github.com/gozhuyinglong/blog-demos.git (fetch)
origin  https://github.com/gozhuyinglong/blog-demos.git (push)

该命令会显示读写远程仓库的名称和地址,我这里指向的是Github。

1.2 远程仓库重命名

既然这个地址是Github,为了好识别,就将名称改为 github 吧。输入命令:
git remote rename <old_remote> <new_remote>

git remote rename origin github

输入查看远程仓库命令,验证下是否成功,输出结果:

github  https://github.com/gozhuyinglong/blog-demos.git (fetch)
github  https://github.com/gozhuyinglong/blog-demos.git (push)

成功!

1.3 添加另外一个远程仓库

下面咱们再添加Gitee上的远程仓库,首先在Gitee上建立一个空的仓库,名称与Github上相同。

而后在【克隆/下载】处复制地址。

输出添加远程仓库命令:
git remote add <remote> <url>

git remote add gitee https://gitee.com/gozhuyinglong/blog-demos.git

再来验证下是否成功,输出结果:

gitee   https://gitee.com/gozhuyinglong/blog-demos.git (fetch)
gitee   https://gitee.com/gozhuyinglong/blog-demos.git (push)
github  https://github.com/gozhuyinglong/blog-demos.git (fetch)
github  https://github.com/gozhuyinglong/blog-demos.git (push)

成功!

1.4 多个远程仓库的推送/拉取

有了多个远程仓库,推送和拉取不再能像之前那样git pushgit 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

1.5 移除一个远程仓库

若是想要移除一个远程仓库,可使用下面命令:
git remote remove <remote>git remote rm <remote>

git remote remove gitee

执行移除远程仓库后,该仓库在本地的全部分支与配置信息也会一并删除。

2. 常见错误及解决方案

在执行上面操做固然不是一路顺风的,若是你遇到一些错误,这里可能有你想要的答案。

2.1 提示未指定分支

当在拉取时报下面错误:

You asked to pull from the remote 'gitee', but did not specify
a branch. Because this is not 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

2.2 没有存储库的权限

当执行推送操做时,提示下面信息:

remote: You do not have permission push to this repository
fatal: unable to access 'https://gitee.com/gozhuyinglong/blog-demos.git/': The requested URL returned error: 403

表示没有远程仓库的权限,应该首先查看远程仓库是否公开,再检查本地帐号和密码是否正确。

2.3 远程仓库未公开

登陆Gitee,检查该代码库是否公司。若未公开,则设置为公开。

2.4 Windows凭据中的帐号和密码错误

打开控制面板,点击【用户帐户】

再点击【管理Windows凭据】

找到你的帐号,修改帐号和密码便可。

2.5 删除Windows凭据,从新登陆

你也能够直接将Windows凭据删掉,当执行推送命令后,会弹出Windows安全中心登陆框。

输入你的帐号或密码就能够了。

2.6 没法弹出Windows安全中心登陆

若是没法弹出Windows安全中心登陆框,则将其卸载掉,执行下面命令:

git credential-manager uninstall

再从新下载并安装,下载地址:
https://github.com/microsoft/Git-Credential-Manager-for-Windows/releases

2.7 每次推送Github时弹出登陆框,可使用SSH地址

以下图所示,当你每次push代码时,都会弹出下面登陆框。

咱们能够将远程地址改成SSH地址:

移除如今的github地址,从新添加ssh地址,具体代码参照上文。

添加好地址后,还须要在github上设置SSH Key

2.8 生成SSH Key,并添加到Github

输入下面命令来生成SSH Key,双引号内填写你的登陆邮箱地址

ssh-keygen -t rsa -C "xxxxx@xxxxx.com"

若是弹出下面提示,直接回车便可。(若已存在,会提示是否替换,输入Y再回车)

会在你的磁盘目录【C:\Users\你的用户名.ssh\】中生成公钥,将文件【id_rsa.pub】中的内存拷贝。

打开github的【SSH and GPG keys】页面,添加便可:

往期推荐

相关文章
相关标签/搜索