如何將代碼同時提交到Github和碼雲Gitee上

主頁:gozhuyinglong.github.iogit

Gitee:gitee.com/gozhuyinglo…github

Github:github.com/gozhuyinglo…web

微信搜索:碼農StayUpshell

相信不少寫開源項目的小夥伴都會將代碼託管到Github上,但隨着近些年碼雲Gitee的火熱,也有很多用戶選擇碼雲作爲遠程倉庫。爲了提升開源項目的曝光度,會選擇將代碼同時在兩個平臺進行託管。安全

那麼如何將代碼同時提交到Github和Gitee上呢?本文將進行詳細介紹,並列出常見錯誤及解決方案。微信

本文目錄:markdown

1. 多個遠程倉庫的使用

多個遠程倉庫在項目中不多使用,但Git自己是支持的。ssh

那讓咱們跟着一個案例來溫習一下Git命令吧:案例代碼已經在Github中託管了,如今要增長Gitee遠程倉庫。oop

1.1 查看遠程倉庫

先來查看下當前項目的遠程倉庫fetch

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 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
複製代碼

再從新下載並安裝,下載地址: github.com/microsoft/G…

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】頁面,添加便可:

往期推薦

相關文章
相關標籤/搜索