这个属于参与开源项目必备技能了🌟
开源项目贡献流程:
graph LR
A[官方仓库 upstream] --> |fork| B[你的仓库 origin]
A --> |更新上游 (本篇文章) | C[本地仓库]
B --> |拉取 clone/pull| C
C --> |推送 git push origin| B
B --> |向 upstream 提 PR| A
upstream首先保证本地仓库的upstream是源项目的URL,若没有则添加
git branch -vv #查看分支状态
git remote add upstream https://github.com/user/repo.git #设置原始上游项目仓库
然后利用fetch和merge合并upstream的main分支
git fetch upstream
git merge upstream/main
此时本地的main分支就更新至upstream的main版本。然后利用push命令将本地分支覆盖到 git 远程分支上
git push origin main:main
有些人可能对远程的操作命令有点迷糊,这里简单解释一下。
origin 与 upstream 的区别origin\
指向你自己的远程仓库(通常是 fork 或直接创建的仓库)。\
这是你推送代码的主仓库,用于:
git push origin maingit pull origin main
# 示例:关联个人仓库
git remote add origin https://github.com/yourname/repo.git
upstream\
指向原始项目仓库(如开源项目官方仓库)。\
这是你同步官方更新的仓库,用于:
git fetch upstream# 示例:关联官方仓库
git remote add upstream https://github.com/official/repo.git
| 特性 | origin (个人仓库) |
upstream (官方仓库) |
|---|---|---|
| 推送权限 | ✅ 可直接推送代码 | ❌ 通常无推送权限(只读) |
| 拉取来源 | 你的个人远程仓库 | 原始项目仓库 |
| 典型操作 | push/pull 日常开发 |
fetch 同步官方更新 |