fork源码库以后如何同步最新的代码

本文介绍了如何通过Git的fork功能将mybatis-3的源码库复制到个人仓库,并通过添加远程仓库(upstream)来同步源代码库的所有分支。详细步骤包括设置关联源仓库、使用gitfetch命令同步特定分支或所有内容到本地仓库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、先fork一个代码库到自己的仓库

比如: 我fork mybatis-3的源码库到自己的仓库(取名为:mybatis-3-note)
这个比较简单,点击fork按钮就行

2、fork的时候,只能fork源代码库的master分支,如何同步源代码库的所有内容到自己的仓库呢?

  • 在自己本地库设置关联源仓库
~/mygithub/mybatis-3-note  master ✔                                                                                                                  4d4h  ⍉
▶ git remote add upstream git@github.com:mybatis/mybatis-3.git

这里的upstream是自定义的名字,随便取名什么都行,目的是标记这个与源代码库建立关联

  • 然后执行命令 git remote -v 查看远程仓库的路径:
~/mygithub/mybatis-3-note  master ✔                                                                                                                   4d5h
▶ git remote -v
origin    git@github.com:xxx/mybatis-3-note.git (fetch)
origin    git@github.com:xxx/mybatis-3-note.git (push)
upstream    git@github.com:mybatis/mybatis-3.git (fetch)
upstream    git@github.com:mybatis/mybatis-3.git (push)

这里发现创建了一个叫做upstream的与源代码origin关联

  • 关联之后,就可以同步源代码库的其他分支到自己的仓库了,具体执行:git fetch [upstream自己前面定义的名字]
    原代码库分支名 : 本地代码库分支名,如同步mybatis-3源码库的3.4.x分支到我的代码库,并且也命名为3.4.x,执行命令:
    git fetch upstream 3.4.x:3.4.x
~/mygithub/mybatis-3-note  master ✔                                                                                                                  4d5h  ⍉
▶ git fetch upstream 3.4.x:3.4.x
remote: Enumerating objects: 77, done.
remote: Counting objects: 100% (16/16), done.
remote: Total 77 (delta 16), reused 16 (delta 16), pack-reused 61
Unpacking objects: 100% (77/77), done.
From github.com:mybatis/mybatis-3
 * [new branch]          3.4.x                  -> 3.4.x
 * [new tag]             mybatis-3.0.1          -> mybatis-3.0.1
 * [new tag]             mybatis-3.0.2          -> mybatis-3.0.2
 * [new tag]             mybatis-3.0.3          -> mybatis-3.0.3
 * [new tag]             mybatis-3.0.4          -> mybatis-3.0.4
 * [new tag]             mybatis-3.0.5          -> mybatis-3.0.5
 * [new tag]             mybatis-3.0.6          -> mybatis-3.0.6
 * [new tag]             mybatis-3.1.0          -> mybatis-3.1.0
 * [new tag]             mybatis-3.1.1          -> mybatis-3.1.1
 * [new tag]             mybatis-3.2.0          -> mybatis-3.2.0
 * [new tag]             mybatis-3.2.1          -> mybatis-3.2.1
 * [new tag]             mybatis-3.2.2          -> mybatis-3.2.2
 * [new tag]             mybatis-3.2.3          -> mybatis-3.2.3
 * [new tag]             mybatis-3.2.4          -> mybatis-3.2.4
 * [new tag]             mybatis-3.2.4-SNAPSHOT -> mybatis-3.2.4-SNAPSHOT
 * [new tag]             mybatis-3.2.5          -> mybatis-3.2.5
 * [new tag]             mybatis-3.2.5-SNAPSHOT -> mybatis-3.2.5-SNAPSHOT
 * [new tag]             mybatis-3.2.6          -> mybatis-3.2.6
 * [new tag]             mybatis-3.2.6-SNAPSHOT -> mybatis-3.2.6-SNAPSHOT
 * [new tag]             mybatis-3.2.7          -> mybatis-3.2.7
 * [new tag]             mybatis-3.3.0          -> mybatis-3.3.0
 * [new tag]             mybatis-3.3.0-SNAPSHOT -> mybatis-3.3.0-SNAPSHOT
 * [new tag]             mybatis-3.4.0          -> mybatis-3.4.0
 * [new tag]             mybatis-3.4.1          -> mybatis-3.4.1
 * [new tag]             mybatis-3.4.2          -> mybatis-3.4.2
 * [new tag]             mybatis-3.4.3          -> mybatis-3.4.3
 * [new tag]             mybatis-3.4.4          -> mybatis-3.4.4
 * [new tag]             mybatis-3.4.5          -> mybatis-3.4.5
 * [new tag]             mybatis-3.4.6          -> mybatis-3.4.6
 * [new tag]             mybatis-3.5.0          -> mybatis-3.5.0
 * [new tag]             mybatis-3.5.1          -> mybatis-3.5.1
 * [new tag]             mybatis-3.5.10         -> mybatis-3.5.10
 * [new tag]             mybatis-3.5.11         -> mybatis-3.5.11
 * [new tag]             mybatis-3.5.12         -> mybatis-3.5.12
 * [new tag]             mybatis-3.5.13         -> mybatis-3.5.13
 * [new tag]             mybatis-3.5.2          -> mybatis-3.5.2
 * [new tag]             mybatis-3.5.3          -> mybatis-3.5.3
 * [new tag]             mybatis-3.5.4          -> mybatis-3.5.4
 * [new tag]             mybatis-3.5.5          -> mybatis-3.5.5
 * [new tag]             mybatis-3.5.6          -> mybatis-3.5.6
 * [new tag]             mybatis-3.5.7          -> mybatis-3.5.7
 * [new tag]             mybatis-3.5.8          -> mybatis-3.5.8
 * [new tag]             mybatis-3.5.9          -> mybatis-3.5.9
 * [new branch]          3.4.x                  -> upstream/3.4.x

  • 或者直接执行 git fetch [upstream自己前面定义的名字] ,将源代码库的所有全部同步到自己的代码库
~/mygithub/mybatis-3-note  master ✔                                                                                                                   4d5h
▶ git fetch upstream

remote: Enumerating objects: 386125, done.
remote: Counting objects: 100% (198/198), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 386125 (delta 195), reused 198 (delta 195), pack-reused 385927
Receiving objects: 100% (386125/386125), 94.05 MiB | 1.93 MiB/s, done.
Resolving deltas: 100% (350860/350860), completed with 98 local objects.
From github.com:mybatis/mybatis-3
 * [new branch]            3.2.x                                          -> upstream/3.2.x
 * [new branch]            3.3.x                                          -> upstream/3.3.x
 * [new branch]            gh-pages                                       -> upstream/gh-pages
 * [new branch]            master                                         -> upstream/master
 * [new branch]            renovate/maven-org.hsqldb-hsqldb-vulnerability -> upstream/renovate/maven-org.hsqldb-hsqldb-vulnerability
 * [new tag]               mybatis-3.2.8                                  -> mybatis-3.2.8
 * [new tag]               mybatis-3.3.1                                  -> mybatis-3.3.1

这就同步到了原代码库的分支到自己的代码库。

### 什么是代码 fork `fork()` 是 Unix 和类 Unix 操作系统中的一个重要系统调用,用于创建一个新的进程。新进程被称为子进程,而原进程则称为父进程。通过 `fork()` 调用,操作系统会复制当前进程的所有资源(包括内存空间、文件描述符等),并为子进程分配独立的 PID[^1]。 当提到 **代码 fork** 的概念时,在软件开发领域还有另一种含义——指开发者从现有的开源项目仓库派生出自己的副本。这种行为常见于 GitHub 或 GitLab 平台上的协作开发场景。例如,ApacheCN 提供了一个 PyTorch 中文文档项目的仓库 https://github.com/apachecn/pytorch-doc-zh ,其他开发者可以对其进行 fork 来获得属于自己的版本以便修改或贡献[^2]。 --- ### 如何进行 fork 操作 #### 方法一:使用 Linux/Unix 系统下的 `fork()` 函数 以下是关于如何在 C 编程语言中实现基本的 `fork()` 功能: ```c #include <stdio.h> #include <unistd.h> int main() { pid_t pid = fork(); // 创建子进程 if (pid == -1) { perror("Fork failed"); return 1; } if (pid == 0) { printf("Child process\n"); // 子进程中执行此部分逻辑 } else { printf("Parent process, child's PID is %d\n", pid); // 父进程中执行该部分逻辑 } while(1); } ``` 上述程序展示了简单的父子进程分离机制。注意每次运行都会打印两行不同的消息分别代表父亲和孩子两个不同身份的过程实例[^4]。 #### 方法二:在线托管平台上的 Fork 操作 对于像 GitHub 这样的平台上完成一个项目的分叉动作非常简单直观: 1. 登录账户访问目标公开存储库页面; 2. 找到右上角按钮点击 “Fork” 即可成功建立自己名下的一份拷贝。 之后按照既定工作流推送更改回原始上游源码树之前先同步最新改动以减少冲突几率[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值