微信搜索:码农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
多个远程仓库在项目中不多使用,但Git自己是支持的。安全
那让咱们跟着一个案例来温习一下Git命令吧:案例代码已经在Github中托管了,如今要增长Gitee远程仓库。微信
先来查看下当前项目的远程仓库ssh
git remote
不出意外,应该会输出:fetch
origin
这个origin
就是一个指向远程仓库的名称,是你在clone
时 git
为你默认建立的。网站
能够经过命令查看origin
指向的远程仓库地址:this
git remote -v
输出结果:
origin https://github.com/gozhuyinglong/blog-demos.git (fetch) origin https://github.com/gozhuyinglong/blog-demos.git (push)
该命令会显示读写远程仓库的名称和地址,我这里指向的是Github。
既然这个地址是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)
成功!
下面咱们再添加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)
成功!
有了多个远程仓库,推送和拉取不再能像之前那样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. 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
当执行推送操做时,提示下面信息:
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
表示没有远程仓库的权限,应该首先查看远程仓库是否公开,再检查本地帐号和密码是否正确。
登陆Gitee,检查该代码库是否公司。若未公开,则设置为公开。
打开控制面板,点击【用户帐户】
再点击【管理Windows凭据】
找到你的帐号,修改帐号和密码便可。
你也能够直接将Windows凭据删掉,当执行推送命令后,会弹出Windows安全中心登陆框。
输入你的帐号或密码就能够了。
若是没法弹出Windows安全中心登陆框,则将其卸载掉,执行下面命令:
git credential-manager uninstall
再从新下载并安装,下载地址:
https://github.com/microsoft/Git-Credential-Manager-for-Windows/releases
以下图所示,当你每次push
代码时,都会弹出下面登陆框。
咱们能够将远程地址改成SSH地址:
移除如今的github地址,从新添加ssh地址,具体代码参照上文。
添加好地址后,还须要在github上设置SSH Key
输入下面命令来生成SSH Key,双引号内填写你的登陆邮箱地址
ssh-keygen -t rsa -C "xxxxx@xxxxx.com"
若是弹出下面提示,直接回车便可。(若已存在,会提示是否替换,输入Y再回车)
会在你的磁盘目录【C:\Users\你的用户名.ssh\】中生成公钥,将文件【id_rsa.pub】中的内存拷贝。
打开github的【SSH and GPG keys】页面,添加便可: