Fork仓库同步源仓库的更新

什么是Fork

Fork 仓库是对应官方源仓库的一份完全备份(类似快照,但却不能再接收到官方源仓库的更新内容),但我们会需要将 Fork 仓库更新至源仓库的最新提交代码

Fork仓库同步源仓库的更新

从官方源仓库同步更新内容到 Fork 仓库的方法,过程大致如下:

  • 向本地 Fork 仓库添加官方源仓库远端
  • 再获取 (fetch) 官方源仓库的更新内容到本地仓库
  • 然后合并官方源仓库的更新内容到本地 Fork 仓库
  • 最后推送到远端 Fork 仓库。

1. 添加官方源仓库远端

1.1. 查看远端仓库

查看本地 Fork 仓库的远端信息

C:\Users\kingl\PycharmProjects\udp>git remote -v
origin  git@10.186.18.21:jinchanglong/udp.git (fetch)
origin  git@10.186.18.21:jinchanglong/udp.git (push)

一般此时查询的结果只有一个 Fork 仓库远端(共两行 fetch + push),其中 "origin"(默认值)为本地设置的 Fork 远端仓库的远端别名, 而 "git@10.186.18.21:jinchanglong/udp.git" 对应为你的 Fork 仓库地址

1.2. 添加官方源仓库远端

添加官方源仓库远端到本地 Fork 仓库,使具有两个远端

C:\Users\kingl\PycharmProjects\udp>git remote add upstream git@10.186.18.21:docs/udp.git

C:\Users\kingl\PycharmProjects\udp>git remote -v
origin  git@10.186.18.21:jinchanglong/udp.git (fetch)
origin  git@10.186.18.21:jinchanglong/udp.git (push)
upstream        git@10.186.18.21:docs/udp.git (fetch)
upstream        git@10.186.18.21:docs/udp.git (push)

其中 "upstream" 为自定义官方源仓库在本地的远端别名, "git@10.186.18.21:docs/udp.git" 为官方源仓库地址

 

2. 获取官方源仓库的更新内容

2.1. 拉取源仓库代码

C:\Users\kingl\PycharmProjects\udp>git fetch upstream

2.2. 检查并切换到想更新的本地分支 (一般为 master 分支)

检查当前分支:

C:\Users\kingl\PycharmProjects\udp>git branch
* master

一般此时查询结果为 origin/master 分支(默认),其对应远端分支为 Fork 远端仓库的 master 分支。  如果当前为非master分支,可先做手动切换(git checkout)

 

3. 合并源仓库的更新到本地

C:\Users\kingl\PycharmProjects\udp>git merge upstream/master

 

4. 推送本地代码到远端 Fork 仓库

此时本地 Fork 仓库的 master 分支已包含官方源仓库 master 分支的更新内容,再执行 push 操作将这些更新内容推送到自己的远端 Fork 仓库即可
C:\Users\kingl\PycharmProjects\udp>git push origin master
Enumerating objects: 91, done.
Counting objects: 100% (79/79), done.
Delta compression using up to 8 threads
Compressing objects: 100% (49/49), done.
Writing objects: 100% (59/59), 10.86 KiB | 794.00 KiB/s, done.
Total 59 (delta 24), reused 45 (delta 10), pack-reused 0
To 10.186.18.21:jinchanglong/udp.git
   68f3566..a9c10fe  master -> master

至此,已完成同步官方源仓库 mater 分支的所有更新到 Fork 仓库。

 

posted @ 2022-07-26 16:15  MMartin  阅读(203)  评论(0)    收藏  举报