Fork工作流

在 Git 中,你可以设置一个额外的远程仓库(通常称为 upstream),用于跟踪原始项目(如开源项目)的更新。这在 Fork 工作流 中非常常见,例如:

  • 你 Fork 了某个仓库(origin 指向你的 Fork)。
  • 你需要同步原始仓库(upstream 指向原始项目)。

1. 添加 upstream 远程仓库

(1) 查看当前远程仓库

复制代码
git remote -v

默认只有 origin(你的 Fork):

scss 复制代码
origin  git@github.com:your-username/repo.git (fetch)
origin  git@github.com:your-username/repo.git (push)

(2) 添加 upstream(原始仓库)

csharp 复制代码
git remote add upstream https://github.com/original-owner/repo.git
  • upstream 是自定义名称(常用,但你可以换成其他名字)。
  • 如果是 SSH 方式(推荐):
scss 复制代码
git remote add upstream git@github.com:original-owner/repo.git

(3) 验证是否添加成功

复制代码
git remote -v

输出:

perl 复制代码
origin    git@github.com:your-username/repo.git (fetch)
origin    git@github.com:your-username/repo.git (push)
upstream  https://github.com/original-owner/repo.git (fetch)
upstream  https://github.com/original-owner/repo.git (push)

2. 从 upstream 拉取更新

(1) 获取 upstream 的最新代码

sql 复制代码
git fetch upstream

(2) 合并 upstream 的更新到你的本地分支

假设你想同步 upstream/main 到你的 main 分支:

css 复制代码
git checkout main          # 切换到你的本地 main 分支
git merge upstream/main   # 合并 upstream 的 main 分支

如果出现冲突,需要手动解决后提交:

sql 复制代码
git add .
git commit -m "Merge upstream changes"

(3) 推送到你的 origin(Fork 仓库)

css 复制代码
git push origin main

3. 更新 upstream 的 URL(如果需要修改)

如果 upstream 地址有变(如从 HTTPS 切换到 SSH):

kotlin 复制代码
git remote set-url upstream git@github.com:original-owner/repo.git

4. 删除 upstream(如果需要)

arduino 复制代码
git remote remove upstream

5. 典型工作流示例

场景:你 Fork 了一个仓库,现在想同步原始仓库的最新代码。

bash 复制代码
# 1. 添加 upstream(原始仓库)
git remote add upstream https://github.com/original-owner/repo.git

# 2. 获取 upstream 的最新代码
git fetch upstream

# 3. 切换到你的本地 main 分支
git checkout main

# 4. 合并 upstream 的 main 分支
git merge upstream/main

# 5. 推送到你的 origin(Fork 仓库)
git push origin main
相关推荐
我的xiaodoujiao8 小时前
从 0 到 1 搭建 Python 语言 Web UI自动化测试学习系列 9--基础知识 5--常用函数 3
前端·python·测试工具·ui
李鸿耀10 小时前
Flex 布局下文字省略不生效?原因其实很简单
前端
皮蛋瘦肉粥_12111 小时前
pink老师html5+css3day06
前端·css3·html5
华仔啊16 小时前
前端必看!12个JS神级简写技巧,代码效率直接飙升80%,告别加班!
前端·javascript
excel16 小时前
dep.ts 逐行解读
前端·javascript·vue.js
爱上妖精的尾巴16 小时前
5-20 WPS JS宏 every与some数组的[与或]迭代(数组的逻辑判断)
开发语言·前端·javascript·wps·js宏·jsa
excel16 小时前
Vue3 响应式核心源码全解析:Dep、Link 与 track/trigger 完整执行机制详解
前端
前端大卫16 小时前
一个关于时区的线上问题
前端·javascript·vue.js
whltaoin17 小时前
中秋赏月互动页面:用前端技术演绎传统节日之美
前端·javascript·html·css3·中秋主题前端
IT派同学17 小时前
TableWiz诞生记:一个被表格合并逼疯的程序员如何自救
前端·vue.js