一、应用场景:
将主仓代码同步到个人的代码仓,以开源github上pytest代码为例,比如开源git地址,fork到个人仓git地址
(1)在本地初始化一个pytest的代码仓,因为还没有和远端代码仓关联,因此这里看不到什么关联关系,git remote -v 即为查看本地仓库和远端仓库的关联关系的
hitre@iscas-redrose2100 MINGW64 /d/src/pytest
$ git init
Initialized empty Git repository in D:/src/pytest/.git/
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$ git remote -v
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$
(2)将本地仓库和远端个人仓和公共代码仓关联,个人仓远端地址名为origin,公共代码仓地址名为upstream,这是通用的名称,当存在更多远端代码仓时,还可以自己定义远端代码仓比如gitee
hitre@iscas-redrose2100 MINGW64 /d/src/pytest
$ git init
Initialized empty Git repository in D:/src/pytest/.git/
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$ git remote -v
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$ git remote add origin git@:redrose2100/pytest.git
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$ git remote -v
origin :redrose2100/pytest.git (fetch)
origin :redrose2100/pytest.git (push)
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$ git remote add upstream :pytest-dev/pytest.git
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$ git remote -v
origin :redrose2100/pytest.git (fetch)
origin :redrose2100/pytest.git (push)
upstream :pytest-dev/pytest.git (fetch)
upstream :pytest-dev/pytest.git (push)
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$ git remote add gitee :redrose2100/pytest.git
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$ git remote -v
gitee git@:redrose2100/pytest.git (fetch)
gitee git@:redrose2100/pytest.git (push)
origin git@:redrose2100/pytest.git (fetch)
origin git@:redrose2100/pytest.git (push)
upstream git@:pytest-dev/pytest.git (fetch)
upstream git@:pytest-dev/pytest.git (push)
hitre@iscas-redrose2100 MINGW64 /d/src/pytest (master)
$
(3) 代码同步:首先本地切换到通远端同一个名称的分支名,然后从origin分支拉取代码,这里我们指导upstream是官方的代码,因此,需要从uptream拉取官方代码,然后合入到本地分支,再从本地分支推送到origin个人远端仓和gitee码云平台的远端仓,这样就做到了github个人代码仓和gitee代码仓通github上官方代码仓代码同步了
git checkout -b main
git pull origin main
git fetch upstream
git merge upstream/maiin
git push origin main
git push gitee main
(4)修改和删除远端分支
git remote set-url origin xxxxx # 将origin远端的url修改为xxxxx
git remote rm origin # 删除origin远端的url