PR 是 Pull Request 的缩写,中文常译为「拉取请求」—— 这是开源协作中核心的代码贡献机制,本质是:
你(贡献者)在自己复刻(Fork)的仓库中完成代码修改后,向原开源项目的作者发起「请求」,希望对方将你的修改代码「拉取」并合并到原项目的主分支中,供所有用户使用。
补充两个易混淆的相关缩写(避免混淆):

  • MR(Merge Request):与 PR 功能完全一致,只是 GitLab、Bitbucket 等平台的命名习惯(GitHub 叫 PR,GitLab 叫 MR);

  • CR(Code Review):PR/MR 发起后,原项目作者对代码的「审查过程」,是 PR 被合并前的必要环节。

PR 是贡献代码的「请求动作」,CR 是审查代码的「过程」,合并(Merge)是 PR 被通过后的「结果」。

本文介绍向开源项目提交 Pull Request (PR) 的完整流程。


📋 流程概览

Fork → Clone → Branch → Code → Commit → Push → PR → Review → Merge

1️⃣ Fork 项目

在 GitHub 上点击目标仓库的 Fork 按钮,将项目复制到你的账户下。

原始仓库: https://github.com/original-owner/project
你的 Fork: https://github.com/your-username/project

2️⃣ Clone 到本地

git clone https://github.com/your-username/project.git
cd project

# 添加上游仓库(原始仓库)
git remote add upstream https://github.com/original-owner/project.git

验证 remote 设置:

git remote -v
# origin    https://github.com/your-username/project.git (fetch)
# origin    https://github.com/your-username/project.git (push)
# upstream  https://github.com/original-owner/project.git (fetch)
# upstream  https://github.com/original-owner/project.git (push)

3️⃣ 同步上游代码

提交 PR 前,确保你的代码是基于最新的上游代码:

git fetch upstream
git checkout main
git merge upstream/main

4️⃣ 创建功能分支

永远不要在 main 分支上直接开发!

git checkout -b <branch-name>

分支命名规范

类型 格式 示例
新功能 feature/<description> feature/add-dark-mode
Bug 修复 fix/<description> fix/login-crash
文档 docs/<description> docs/update-readme
重构 refactor/<description> refactor/api-client
性能 perf/<description> perf/optimize-query

5️⃣ 开发与测试

  1. 编写代码
  2. 遵循项目的代码规范(查看 .editorconfigCONTRIBUTING.md
  3. 运行测试确保没有破坏现有功能
  4. 添加必要的测试用例
# 运行测试(具体命令看项目文档)
npm test        # Node.js 项目
pytest          # Python 项目
go test ./...   # Go 项目

6️⃣ 提交代码

Commit Message 规范

推荐使用 Conventional Commits 规范:

<type>(<scope>): <subject>

<body>

<footer>

Type 类型

Type 说明
feat 新功能
fix Bug 修复
docs 文档更新
style 代码格式(不影响运行)
refactor 重构(无新功能或修复)
perf 性能优化
test 测试相关
chore 构建/工具链/依赖更新

示例

git add .
git commit -m "fix: resolve crash when loading large files

The application crashed when loading files larger than 100MB due to
memory allocation issues.

- Added streaming file reader
- Implemented chunked processing
- Added memory usage monitoring

Fixes #123"

7️⃣ Push 到远程

git push -u origin <branch-name>

8️⃣ 创建 Pull Request

在 GitHub 上操作

  1. 访问你的 Fork 仓库
  2. 点击 “Compare & pull request” 按钮
  3. 填写 PR 信息

PR 标题格式

<type>: <short description>

示例:

  • fix: resolve memory leak in image processing
  • feat: add support for dark mode
  • docs: update installation guide

PR 描述模板

## Summary
Brief description of what this PR does.

## Problem
What problem does this solve? Link to related issues.

## Solution
How did you solve the problem?

## Changes
- Change 1
- Change 2
- Change 3

## Testing
How was this tested?

## Screenshots (if applicable)
Add screenshots for UI changes.

## Checklist
- [ ] Tests added/updated
- [ ] Documentation updated
- [ ] Code follows project style guide
- [ ] No breaking changes (or documented if any)

9️⃣ 处理 Review 反馈

维护者可能会要求修改。直接在同一分支上修改并 push:

# 修改代码后
git add .
git commit -m "fix: address review feedback"
git push

PR 会自动更新。

合并上游更新(如果需要)

git fetch upstream
git rebase upstream/main
git push --force-with-lease

🎉 Merge 完成

PR 被合并后,清理工作:

# 删除本地分支
git checkout main
git branch -d <branch-name>

# 删除远程分支
git push origin --delete <branch-name>

# 同步上游
git pull upstream main
git push origin main

⚠️ 注意事项

  1. 阅读 CONTRIBUTING.md - 每个项目可能有特殊要求
  2. 保持 PR 小而专注 - 一个 PR 只做一件事
  3. 写好描述 - 帮助维护者理解你的改动
  4. 耐心等待 - 维护者可能很忙,不要频繁催促
  5. 礼貌沟通 - 开源社区靠互相尊重维系

🔗 有用的链接

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐