GitHub Pull Request Workflow
GitHub Pull Request 工作流
Complete guide for managing the PR lifecycle. Each section shows the
way first, then the
+
fallback for machines without
.
管理PR生命周期的完整指南。每个章节先展示使用
的方式,再展示针对未安装
的机器,使用
+
的替代方案。
- Authenticated with GitHub (see skill)
- Inside a git repository with a GitHub remote
- 已通过GitHub认证(参考技能)
- 处于包含GitHub远程仓库的git仓库目录中
Quick Auth Detection
快速认证检测
Determine which method to use throughout this workflow
Determine which method to use throughout this workflow
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
AUTH="gh"
else
AUTH="git"
Ensure we have a token for API calls
if [ -z "$GITHUB_TOKEN" ]; then
GITHUB_TOKEN=$(grep "github.com" ~/.git-credentials 2>/dev/null | head -1 | sed 's|https://[^:]:([^@])@.*|\1|')
fi
fi
echo "Using: $AUTH"
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
AUTH="gh"
else
AUTH="git"
Ensure we have a token for API calls
if [ -z "$GITHUB_TOKEN" ]; then
GITHUB_TOKEN=$(grep "github.com" ~/.git-credentials 2>/dev/null | head -1 | sed 's|https://[^:]:([^@])@.*|\1|')
fi
fi
echo "Using: $AUTH"
Extracting Owner/Repo from the Git Remote
从Git远程仓库提取所有者/仓库信息
Many
commands need
. Extract it from the git remote:
Works for both HTTPS and SSH remote URLs
Works for both HTTPS and SSH remote URLs
REMOTE_URL=$(git remote get-url origin)
OWNER_REPO=$(echo "$REMOTE_URL" | sed -E 's|.*github.com[:/]||; s|.git$||')
OWNER=$(echo "$OWNER_REPO" | cut -d/ -f1)
REPO=$(echo "$OWNER_REPO" | cut -d/ -f2)
echo "Owner: $OWNER, Repo: $REPO"
REMOTE_URL=$(git remote get-url origin)
OWNER_REPO=$(echo "$REMOTE_URL" | sed -E 's|.*github.com[:/]||; s|.git$||')
OWNER=$(echo "$OWNER_REPO" | cut -d/ -f1)
REPO=$(echo "$OWNER_REPO" | cut -d/ -f2)
echo "Owner: $OWNER, Repo: $REPO"
1. Branch Creation
1. 分支创建
This part is pure
— identical either way:
Make sure you're up to date
Make sure you're up to date
git fetch origin
git checkout main && git pull origin main
git fetch origin
git checkout main && git pull origin main
Create and switch to a new branch
Create and switch to a new branch
git checkout -b feat/add-user-authentication
Branch naming conventions:
- `feat/description` — new features
- `fix/description` — bug fixes
- `refactor/description` — code restructuring
- `docs/description` — documentation
- `ci/description` — CI/CD changes
git checkout -b feat/add-user-authentication
分支命名规范:
- `feat/description` — 新功能
- `fix/description` — Bug修复
- `refactor/description` — 代码重构
- `docs/description` — 文档更新
- `ci/description` — CI/CD变更
Use the agent's file tools (
,
) to make changes, then commit:
使用Agent的文件工具(
、
)修改代码,然后提交:
Stage specific files
Stage specific files
git add src/auth.py src/models/user.py tests/test_auth.py
git add src/auth.py src/models/user.py tests/test_auth.py
Commit with a conventional commit message
Commit with a conventional commit message
git commit -m "feat: add JWT-based user authentication
- Add login/register endpoints
- Add User model with password hashing
- Add auth middleware for protected routes
- Add unit tests for auth flow"
Commit message format (Conventional Commits):
type(scope): short description
Longer explanation if needed. Wrap at 72 characters.
Types: `feat`, `fix`, `refactor`, `docs`, `test`, `ci`, `chore`, `perf`
git commit -m "feat: add JWT-based user authentication
- Add login/register endpoints
- Add User model with password hashing
- Add auth middleware for protected routes
- Add unit tests for auth flow"
提交消息格式(Conventional Commits):
type(scope): short description
Longer explanation if needed. Wrap at 72 characters.
类型:`feat`, `fix`, `refactor`, `docs`, `test`, `ci`, `chore`, `perf`
3. Pushing and Creating a PR
3. 推送分支并创建PR
Push the Branch (same either way)
推送分支(两种方式一致)
bash
git push -u origin HEAD
bash
git push -u origin HEAD
With gh:
bash
gh pr create \
--title "feat: add JWT-based user authentication" \
--body "## Summary
- Adds login and register API endpoints
- JWT token generation and validation
使用gh:
bash
gh pr create \
--title "feat: add JWT-based user authentication" \
--body "## Summary
- Adds login and register API endpoints
- JWT token generation and validation
Closes #42"
Options: `--draft`, `--reviewer user1,user2`, `--label "enhancement"`, `--base develop`
**With git + curl:**
```bash
BRANCH=$(git branch --show-current)
curl -s -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$OWNER/$REPO/pulls \
-d "{
\"title\": \"feat: add JWT-based user authentication\",
\"body\": \"## Summary\nAdds login and register API endpoints.\n\nCloses #42\",
\"head\": \"$BRANCH\",
\"base\": \"main\"
}"
The response JSON includes the PR
— save it for later commands.
To create as a draft, add
to the JSON body.
Closes #42"
可选参数:`--draft`, `--reviewer user1,user2`, `--label "enhancement"`, `--base develop`
**使用git + curl:**
```bash
BRANCH=$(git branch --show-current)
curl -s -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/$OWNER/$REPO/pulls \
-d "{
\"title\": \"feat: add JWT-based user authentication\",
\"body\": \"## Summary\nAdds login and register API endpoints.\n\nCloses #42\",
\"head\": \"$BRANCH\",
\"base\": \"main\"
}"
返回的JSON包含PR的
——保存该值用于后续命令。
4. Monitoring CI Status
4. 监控CI状态
One-shot check
One-shot check
Watch until all checks finish (polls every 10s)
Watch until all checks finish (polls every 10s)
gh pr checks --watch
**With git + curl:**
```bash
gh pr checks --watch
**使用git + curl:**
```bash
Get the latest commit SHA on the current branch
Get the latest commit SHA on the current branch
SHA=$(git rev-parse HEAD)
SHA=$(git rev-parse HEAD)
Query the combined status
Query the combined status
curl -s
-H "Authorization: token $GITHUB_TOKEN"
https://api.github.com/repos/$OWNER/$REPO/commits/$SHA/status
| python3 -c "
import sys, json
data = json.load(sys.stdin)
print(f"Overall: {data['state']}")
for s in data.get('statuses', []):
print(f" {s['context']}: {s['state']} - {s.get('description', '')}")"
curl -s
-H "Authorization: token $GITHUB_TOKEN"
https://api.github.com/repos/$OWNER/$REPO/commits/$SHA/status
| python3 -c "
import sys, json
data = json.load(sys.stdin)
print(f"Overall: {data['state']}")
for s in data.get('statuses', []):
print(f" {s['context']}: {s['state']} - {s.get('description', '')}")"
Also check GitHub Actions check runs (separate endpoint)
Also check GitHub Actions check runs (separate endpoint)
Poll Until Complete (git + curl)
轮询直至完成(git + curl)
Simple polling loop — check every 30 seconds, up to 10 minutes
Simple polling loop — check every 30 seconds, up to 10 minutes
SHA=$(git rev-parse HEAD)
for i in $(seq 1 20); do
STATUS=$(curl -s
-H "Authorization: token $GITHUB_TOKEN"
https://api.github.com/repos/$OWNER/$REPO/commits/$SHA/status
| python3 -c "import sys,json; print(json.load(sys.stdin)['state'])")
echo "Check $i: $STATUS"
if [ "$STATUS" = "success" ] || [ "$STATUS" = "failure" ] || [ "$STATUS" = "error" ]; then
break
fi
sleep 30
done
SHA=$(git rev-parse HEAD)
for i in $(seq 1 20); do
STATUS=$(curl -s
-H "Authorization: token $GITHUB_TOKEN"
https://api.github.com/repos/$OWNER/$REPO/commits/$SHA/status
| python3 -c "import sys,json; print(json.load(sys.stdin)['state'])")
echo "Check $i: $STATUS"
if [ "$STATUS" = "success" ] || [ "$STATUS" = "failure" ] || [ "$STATUS" = "error" ]; then
break
fi
sleep 30
done
5. Auto-Fixing CI Failures
5. 自动修复CI失败
When CI fails, diagnose and fix. This loop works with either auth method.
当CI失败时,诊断并修复问题。以下流程适用于两种认证方式。
Step 1: Get Failure Details
步骤1:获取失败详情
List recent workflow runs on this branch
List recent workflow runs on this branch
gh run list --branch $(git branch --show-current) --limit 5
gh run list --branch $(git branch --show-current) --limit 5
View failed logs
View failed logs
gh run view <RUN_ID> --log-failed
**With git + curl:**
```bash
BRANCH=$(git branch --show-current)
gh run view <RUN_ID> --log-failed
**使用git + curl:**
```bash
BRANCH=$(git branch --show-current)
List workflow runs on this branch
List workflow runs on this branch
Get failed job logs (download as zip, extract, read)
Get failed job logs (download as zip, extract, read)
Step 2: Fix and Push
步骤2:修复并推送
After identifying the issue, use file tools (
,
) to fix it:
bash
git add <fixed_files>
git commit -m "fix: resolve CI failure in <check_name>"
git push
bash
git add <fixed_files>
git commit -m "fix: resolve CI failure in <check_name>"
git push
Re-check CI status using the commands from Section 4 above.
Auto-Fix Loop Pattern
自动修复循环模式
When asked to auto-fix CI, follow this loop:
- Check CI status → identify failures
- Read failure logs → understand the error
- Use + / → fix the code
git add . && git commit -m "fix: ..." && git push
- Wait for CI → re-check status
- Repeat if still failing (up to 3 attempts, then ask the user)
当需要自动修复CI时,遵循以下循环:
- 检查CI状态 → 定位失败项
- 查看失败日志 → 理解错误原因
- 使用 + / → 修复代码
git add . && git commit -m "fix: ..." && git push
- 等待CI执行 → 重新检查状态
- 若仍失败则重复(最多3次,之后询问用户)
Squash merge + delete branch (cleanest for feature branches)
Squash merge + delete branch (cleanest for feature branches)
gh pr merge --squash --delete-branch
gh pr merge --squash --delete-branch
Enable auto-merge (merges when all checks pass)
Enable auto-merge (merges when all checks pass)
gh pr merge --auto --squash --delete-branch
**With git + curl:**
```bash
PR_NUMBER=<number>
gh pr merge --auto --squash --delete-branch
**使用git + curl:**
```bash
PR_NUMBER=<number>
Merge the PR via API (squash)
Merge the PR via API (squash)
Delete the remote branch after merge
Delete the remote branch after merge
BRANCH=$(git branch --show-current)
git push origin --delete $BRANCH
BRANCH=$(git branch --show-current)
git push origin --delete $BRANCH
Switch back to main locally
Switch back to main locally
git checkout main && git pull origin main
git branch -d $BRANCH
Merge methods: `"merge"` (merge commit), `"squash"`, `"rebase"`
git checkout main && git pull origin main
git branch -d $BRANCH
合并方式:`"merge"`(合并提交)、`"squash"`(压缩合并)、`"rebase"`(变基合并)
Enable Auto-Merge (curl)
启用自动合并(curl)
Auto-merge requires the repo to have it enabled in settings.
Auto-merge requires the repo to have it enabled in settings.
This uses the GraphQL API since REST doesn't support auto-merge.
This uses the GraphQL API since REST doesn't support auto-merge.
7. Complete Workflow Example
7. 完整工作流示例
1. Start from clean main
1. Start from clean main
git checkout main && git pull origin main
git checkout main && git pull origin main
git checkout -b fix/login-redirect-bug
git checkout -b fix/login-redirect-bug
3. (Agent makes code changes with file tools)
3. (Agent makes code changes with file tools)
git add src/auth/login.py tests/test_login.py
git commit -m "fix: correct redirect URL after login
Preserves the ?next= parameter instead of always redirecting to /dashboard."
git add src/auth/login.py tests/test_login.py
git commit -m "fix: correct redirect URL after login
Preserves the ?next= parameter instead of always redirecting to /dashboard."
6. Create PR (picks gh or curl based on what's available)
6. Create PR (picks gh or curl based on what's available)
... (see Section 3)
... (see Section 3)
7. Monitor CI (see Section 4)
7. Monitor CI (see Section 4)
8. Merge when green (see Section 6)
8. Merge when green (see Section 6)
Useful PR Commands Reference
实用PR命令参考
| Action | gh | git + curl |
|---|
| List my PRs | | curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/$OWNER/$REPO/pulls?state=open"
|
| View PR diff | | (local) or curl -H "Accept: application/vnd.github.diff" ...
|
| Add comment | gh pr comment N --body "..."
| curl -X POST .../issues/N/comments -d '{"body":"..."}'
|
| Request review | gh pr edit N --add-reviewer user
| curl -X POST .../pulls/N/requested_reviewers -d '{"reviewers":["user"]}'
|
| Close PR | | curl -X PATCH .../pulls/N -d '{"state":"closed"}'
|
| Check out someone's PR | | git fetch origin pull/N/head:pr-N && git checkout pr-N
|
| 操作 | gh | git + curl |
|---|
| 列出我的PR | | curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/$OWNER/$REPO/pulls?state=open"
|
| 查看PR差异 | | (本地)或 curl -H "Accept: application/vnd.github.diff" ...
|
| 添加评论 | gh pr comment N --body "..."
| curl -X POST .../issues/N/comments -d '{"body":"..."}'
|
| 请求评审 | gh pr edit N --add-reviewer user
| curl -X POST .../pulls/N/requested_reviewers -d '{"reviewers":["user"]}'
|
| 关闭PR | | curl -X PATCH .../pulls/N -d '{"state":"closed"}'
|
| 检出他人的PR | | git fetch origin pull/N/head:pr-N && git checkout pr-N
|