主頁:gozhuyinglong.github.iogit
Gitee:gitee.com/gozhuyinglo…github
Github:github.com/gozhuyinglo…web
微信搜索:碼農StayUpshell
相信不少寫開源項目的小夥伴都會將代碼託管到Github上,但隨着近些年碼雲Gitee的火熱,也有很多用戶選擇碼雲作爲遠程倉庫。爲了提升開源項目的曝光度,會選擇將代碼同時在兩個平臺進行託管。安全
那麼如何將代碼同時提交到Github和Gitee上呢?本文將進行詳細介紹,並列出常見錯誤及解決方案。微信
本文目錄:markdown
多個遠程倉庫在項目中不多使用,但Git自己是支持的。ssh
那讓咱們跟着一個案例來溫習一下Git命令吧:案例代碼已經在Github中託管了,如今要增長Gitee遠程倉庫。oop
先來查看下當前項目的遠程倉庫fetch
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。
既然這個地址是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
複製代碼
再從新下載並安裝,下載地址: github.com/microsoft/G…
以下圖所示,當你每次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】頁面,添加便可: