Skip to content

拉取请求

打开 and reviewing 拉取请求 on GitHub — collaboration workflow 那个 powers 打开源.

你将学到

  • 如何 创建拉取请求从 feature 分支
  • PR review workflow: comments, approvals, and 更改
  • Forking and contributing 到仓库你 做不 own
  • Squash, 合并, and rebase 合并策略 on GitHub

概念讲解

What Is a Pull Request?

A pull request (PR) is a proposal to merge changes from one branch into another. It is the primary collaboration mechanism on GitHub, GitLab, and Bitbucket. Instead of pushing directly to 主要, you push to a feature branch and open a PR for review.

feature-branch → PR → review → merge → main

PRs are not a Git feature — they are a platform feature built on top of Git branches. The Git command is just git 合并; the PR adds the review and discussion layer.

Creating a Pull Request

# Create and push a feature branch
git checkout -b feature-user-auth
# ... make changes ...
git add .
git commit -m "feat: 添加用户认证"
git push -u origin feature-user-auth

Then go to GitHub and click "Compare & 拉取请求". Write a title and description, then click "创建拉取请求".

GitHub also offers a CLI:

gh pr create --title "添加用户认证" --body "Implements JWT-based 认证"

The PR Description

A good PR description includes:

  • What — what does this PR do?
  • Why — why is this change needed? Link to the issue.
  • How — how did you implement it? Highlight key decisions.
  • Testing — how did you test it? What should the reviewer check?
  • Screenshots — for UI changes, before/after screenshots
## What
Adds JWT-based user authentication with login and logout endpoints.

## Why
Closes #42. Users currently cannot log in.

## How
- Added /api/login and /api/logout routes
- JWT tokens stored in httpOnly cookies
- Password hashing with bcrypt

## Testing
- [x] Unit tests for auth middleware
- [x] Integration test for login flow
- [ ] Manual test in staging

The Review Workflow

  1. Open the PR on GitHub
  2. Review — reviewers see the diff, can comment on specific lines
  3. Request changes — reviewer asks for modifications
  4. Update — push more commits to the feature branch (the PR updates automatically)
  5. Approve — reviewer approves the PR
  6. Merge — merge the PR into the target branch
  7. Delete the feature branch (GitHub offers this after merge)

Merge Strategies

GitHub offers three merge strategies:

  • Create a merge commit — preserves all commits + adds a merge commit (default)
  • Squash and merge — combines all feature commits into one commit on main
  • Rebase and merge — replays feature commits onto main without a merge commit

Squash merge is popular for keeping main's history clean — one commit per PR.

Forking Workflow

For repositories you do not have write access to (like open-source projects):

# 1. Fork the repo on GitHub (via the website)

# 2. Clone YOUR fork
git clone https://github.com/YOUR-username/project.git
cd project

# 3. Add the original repo as 'upstream'
git remote add upstream https://github.com/ORIGINAL-username/project.git

# 4. Create a feature branch
git checkout -b fix-typo

# 5. Make changes, commit, push to YOUR fork
git push -u origin fix-typo

# 6. Open a PR on GitHub from YOUR fork to the original repo

Keeping Your Fork Updated

git fetch upstream
git checkout main
git merge upstream/main
git push origin main

Pull requests are where code quality is maintained through peer review. Always review PRs carefully — this is your last chance to catch bugs before they reach main.

示例代码

              # --- Standard PR workflow (you have write access) ---
git checkout -b feature-payment-integration
# ... implement the feature ...
git add .
git commit -m "feat: integrate Stripe payment gateway"
git push -u origin feature-payment-integration

# Open PR via GitHub CLI
gh pr create --title "Integrate Stripe payment" --body "Adds Stripe checkout and webhook handling."
gh pr view --web  # open the PR in your browser

# After review, push updates to the same branch
git commit -am "fix: address review feedback"
git push  # PR updates automatically

# Check PR status
gh pr status
gh pr checks  # view CI status

# Merge the PR (via CLI or GitHub website)
gh pr merge --squash  # squash merge
gh pr merge --merge   # merge commit
gh pr merge --rebase  # rebase merge

# --- Forking workflow (open source contribution) ---
git clone https://github.com/YOUR-username/repo.git
cd repo
git remote add upstream https://github.com/ORIGINAL-username/repo.git
git checkout -b fix-typo-in-readme
echo "fixed" >> README.md
git commit -am "docs: fix typo in README"
git push -u origin fix-typo-in-readme
# Now open a PR on GitHub from your fork to upstream

# Keep your fork's main in sync with upstream
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
            

Shows 标准 PR workflow 使用 GitHub CLI, pushing review 更新, 检查 CI 状态, and 所有 three 合并策略. 然后 covers forking workflow for 打开-源 contributions and keeping 你的 Fork synchronized 使用 upstream.

动手试一试

常见错误

错误写法

打开 PR 使用 huge, unfocused diff 那个是 困难到 review

正确做法

Keep PRs 小 and focused on one concern. 50-行 PR 获取 reviewed 快速地; 5000-行 PR 获取 rubber-stamped or ignored. 如果你的 feature 是大, 拆分它 到多个 smaller PRs. Use feature 分支 and 合并它们 incrementally.

错误写法

不编写 PR 描述 and 制作 reviewers figure out 更改

正确做法

总是编写清晰 PR 描述解释什么, 为什么, and 如何. 链接到 relevant issues. 注意什么 reviewer 应该 focus on. 好描述速度上 review and improves code quality. Use PR template 如果你的仓库有 one.

相关速查表

练习 拉取请求

3 个练习

练习 拉取请求

查阅不认识的术语

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

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

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

查看错误