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

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

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

本文目录:

1. 多个近程仓库的应用

多个近程仓库在我的项目中很少应用,但Git自身是反对的。

那让咱们跟着一个案例来复习一下Git命令吧:案例代码曾经在Github中托管了,当初要减少Gitee近程仓库。

1.1 查看近程仓库

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

git remote

不出意外,应该会输入:

origin

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

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

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 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

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 specifya branch. Because this is not the default configured remotefor 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 repositoryfatal: 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】页面,增加即可:

往期举荐

  • 《 教你应用GitHub搭建集体网站 》