Skip to content

拉取请求 Simulation

中等 约 30 分钟

你将构建什么

局部 simulation 的拉取请求 workflow using bare remote as 'upstream' 仓库. 你将 play 两者都 contributor and maintainer roles.

学习目标

  • Simulate forking and cloning 项目
  • 创建 feature 分支使用更改
  • 推送分支 and simulate PR review
  • 地址 review feedback 使用 additional 提交
  • 合并 feature and clean 上分支

前置要求

  • git 分支, 合并, 推送, 拉取 (Remote Collaboration Simulation)

步骤

  1. 1

    设置 upstream 仓库

    创建 bare 仓库到 act as upstream 项目 (如 GitHub 仓库你 想要到 contribute 到). 添加初始项目.

    代码
    mkdir pr-simulation
    cd pr-simulation
    git init --bare upstream.git
    git clone upstream.git maintainer
    cd maintainer
    echo "# Contributable Project" > README.md
    echo "console.log('app')" > app.js
    git add .
    git commit -m "Initial project"
    git push origin main

    预期结果

    upstream 仓库使用初始项目, cloned as maintainer.

  2. 2

    Contributor clones and 创建 feature 分支

    As contributor, 克隆 upstream, 创建 feature 分支, and 制作更改. 这个 simulates 开始的 contribution.

    代码
    cd ../
    git clone upstream.git contributor
    cd contributor
    git checkout -b feature/add-tests
    echo "// test: app runs" > app.test.js
    git add .
    git commit -m "Add test for app.js"

    预期结果

    Contributor 有 feature 分支使用新 测试 file.

  3. 3

    Contributor pushes feature 分支

    推送 feature 分支到 remote. On GitHub, 这个是 当你 会打开拉取请求. 分支是 现在可见到 maintainer.

    代码
    git push -u origin feature/add-tests

    预期结果

    feature 分支是 pushed 到 remote.

  4. 4

    Maintainer reviews and 请求更改

    maintainer 获取 feature 分支, reviews 它, and 请求更改 (e.g., 添加描述到 测试). 这个 simulates PR review feedback.

    代码
    cd ../maintainer
    git fetch origin
    git checkout feature/add-tests
    cat app.test.js  # review the test
    # Request: add a description comment

    预期结果

    Maintainer 有 reviewed feature 分支 and requested 更改.

  5. 5

    Contributor 地址 feedback and pushes 再次

    后面 as contributor, 地址 review feedback 通过添加描述, 提交, and 推送. PR 是 automatically 更新.

    代码
    cd ../contributor
    echo "// Test: verifies that app.js runs and logs 'app'" > app.test.js
    echo "// test: app runs" >> app.test.js
    git commit -am "Add test description per review"
    git push origin feature/add-tests

    预期结果

    feature 分支是 更新使用 review feedback.

  6. 6

    Maintainer 合并 PR and cleans 上

    maintainer 获取更新分支, 合并它 到主要, pushes, and 删除 feature 分支. contribution 是完整.

    代码
    cd ../maintainer
    git checkout main
    git fetch origin
    git merge origin/feature/add-tests --no-ff -m "Merge feature/add-tests: add app test with description"
    git push origin main
    # Clean up the feature branch
    git push origin --delete feature/add-tests
    git log --oneline

    预期结果

    主要有 feature 合并; feature 分支是 删除从 remote.

自己动手试试

你学到了什么

你 simulated 完整拉取请求 lifecycle: Fork, 分支, 推送, review, feedback, 更新, 合并, cleanup. 这个是 精确 workflow used 通过每个打开-源 contributor and 每个 team using GitHub.

相关资源

相关课程

相关速查表

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回主题