我的項目需要從 Github
切換到 Gitee
,因為 Github
在國內(nèi) push 非常困難(眾所周知的原因)
下面記錄一下舀透,我 Mac 電腦本地的 Github
倉庫是如何遷移到 Gitee
上的恶耽。
Gitee 倉庫準(zhǔn)備
首先密任,需要在 Gitee
上新建一個倉庫,這里過程略過偷俭。
建完了之后浪讳,Gitee
地址:https://gitee.com/superzdd/my-website.git
同時,Github
地址:https://github.com/superzdd/my-website.git
清除本地倉庫
先檢查涌萤,查看本地地址是否是關(guān)聯(lián) Github
的
# 顯示本地關(guān)聯(lián)的遠(yuǎn)程倉庫地址
git remote -v
# 輸出內(nèi)容如下
# 說明我的項目環(huán)境內(nèi)淹遵,存在origin的本地倉庫名
origin https://github.com/superzdd/my-website.git (fetch)
origin https://github.com/superzdd/my-website.git (push)
進(jìn)行清除
# 注意口猜,origin或者取決于你的本地倉庫名叫什么
git remote rm origin
清除之后,再查看一次
git remote -v # 這是如果不輸出任何內(nèi)容透揣,那就是切換成功了
切換到遠(yuǎn)端 Gitee 地址
# 后面兩個參數(shù)要修改成對應(yīng)的項目
# my-website是倉庫名济炎,這個倉庫名是本地的,不需要和遠(yuǎn)端倉庫同名
# https://gitee.com/superzdd/my-website.git 是Gitee的地址
git remote add my-website https://gitee.com/superzdd/my-website.git
git remote -v # 檢查切換是否成功辐真,切換成功后應(yīng)該會顯示這些內(nèi)容
# 輸出如下
my-website https://gitee.com/superzdd/my-website.git (fetch)
my-website https://gitee.com/superzdd/my-website.git (push)
PS:本地倉庫和遠(yuǎn)端倉庫不需要同名
git remote add origin https://gitee.com/superzdd/my-website.git
git push -u origin "main"
選擇遠(yuǎn)端分支
切換完地址之后须尚,直接進(jìn)行 git pull
時仍然會報錯的,因為還沒有選擇遠(yuǎn)端分支:
# 獲取所有分支
git fetch my-website
# 輸出如下
From https://gitee.com/superzdd/my-website
* [new branch] main -> my-website/main
# 選擇分支
# 這里也有兩個參數(shù)
# my-website是倉庫名
# main代表分支名侍咱,這個名字需要在Gitee上確認(rèn)耐床,但一般情況下都是main,main是Gitee上的默認(rèn)主分支的名字
git branch --set-upstream-to=my-website main
注意:如果剛才設(shè)置的本地倉庫名為
origin
,那上面這句話要修改為:
# 查看遠(yuǎn)端分支
>>> git branch -r
origin/dependabot/npm_and_yarn/tvp-vue-demo/ws-6.2.2
origin/dependabot/npm_and_yarn/tvp-vue-demo/y18n-3.2.2
origin/master
# 將本地的origin/master 關(guān)聯(lián)到遠(yuǎn)端origin/master分支
>>> git branch --set-upstream-to=origin/master
# 如果遠(yuǎn)端分支叫main
>>> git branch --set-upstream-to=origin/main
Git pull 拉取代碼
這時所有的切換動作已經(jīng)都做完了楔脯,可以嘗試?yán)〈a
git pull
錯誤情況排查
錯誤日志:切換到遠(yuǎn)程地址時撩轰,倉庫名沒寫
git remote add https://gitee.com/superzdd/my-website.git
# 輸出如下
usage: git remote add [<options>] <name> <url>
-f, --fetch fetch the remote branches
--tags import all tags and associated objects when fetching
or do not fetch any tag at all (--no-tags)
-t, --track <branch> branch(es) to track
-m, --master <branch>
master branch
--mirror[=(push|fetch)]
set up remote as a mirror to push to or fetch from
這個錯誤信息表明你使用 git remote add
命令時沒有提供正確的參數(shù)。git remote add
命令用于添加一個新的遠(yuǎn)程倉庫地址昧廷,其格式為 git remote add <name> <url>
钧敞。
錯誤日志:pull前未fetch
git branch --set-upstream-to=my-website main
# 輸出如下
error: the requested upstream branch 'my-website' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.
這個錯誤信息表明你嘗試設(shè)置一個上游分支,但是遠(yuǎn)程倉庫中沒有名為 main
的分支麸粮。你需要先確保遠(yuǎn)程倉庫中存在你要設(shè)置的分支溉苛。
你可以通過運(yùn)行 git fetch
命令來獲取遠(yuǎn)程倉庫的最新分支信息。這將從遠(yuǎn)程倉庫下載所有分支的信息弄诲,但不會自動合并或修改你的本地分支愚战。
git fetch my-website
運(yùn)行這個命令后,再次嘗試執(zhí)行 git branch --set-upstream-to=my-website main
命令齐遵,應(yīng)該就不會再出現(xiàn)這個錯誤了寂玲。
錯誤日志:pull 前未選擇分支
git pull
# 輸出如下
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=<remote>/<branch> main
這個錯誤信息表明你正在嘗試從遠(yuǎn)程倉庫拉取代碼,但是當(dāng)前分支沒有設(shè)置跟蹤分支梗摇。換句話說拓哟,Git 不知道應(yīng)該從哪個遠(yuǎn)程分支拉取代碼。
要解決這個問題伶授,你可以按照錯誤信息中的提示断序,使用 git branch --set-upstream-to
命令來設(shè)置跟蹤分支。你需要將 <remote>/<branch>
替換為你要拉取的遠(yuǎn)程分支的完整名稱糜烹。
附錄
日志:從 Gitee 反切 Github
### 下方日志 >>>代表我的輸入违诗,其他內(nèi)容為輸出
>>> git remote -v
my-website https://gitee.com/superzdd/my-website.git (fetch)
my-website https://gitee.com/superzdd/my-website.git (push)
origin https://gitee.com/superzdd/my-website.git (fetch)
origin https://gitee.com/superzdd/my-website.git (push)
>>> git remote rm origin
>>> git remote rm my-website
>>> git remote -v
origin
# 原地有個origin一直清理不掉
# 就打算用這個origin 掛載到github上
>>> git remote rm origin
fatal: No such remote: 'origin'
>>> git remote add origin https://github.com/superzdd/my-website.git
>>> git remote -v
origin https://github.com/superzdd/my-website.git (fetch)
origin https://github.com/superzdd/my-website.git (push)
切換好分支以后,關(guān)閉本地 vpn疮蹦,然后重啟電腦
git pull # 成功
切回來的時候诸迟,并沒有fetch
和branch
就能用了,估計是切到origin
分支的原因。
至此Gitee
和Github
倉庫互相切換全部完畢阵苇。