
主頁: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就是一個指向遠程倉庫的名稱,是你在clone時 git 為你默認創建的,
可以通過命令查看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 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
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】頁面,添加即可:

往期推薦
- 《 教你使用GitHub搭建個人網站 》

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/261416.html
標籤:其他
